I'm using the officer
package in R to create tables and figures that output to a Word document. I want to a "Page X of N" field in the header. The following code does what I want it to.
library(officer)
library(flextable)
doc = read_docx()
my_header = flextable(data = data.frame(V1 = c("Protocol", "Compound"),
V2 = c("Company" ,"Page"))) |>
delete_part(part = "header") |>
theme_booktabs() |>
border_remove() |>
align(j = 1, align = "left", part = "body") |>
align(j = 2, align = "right", part = "body") |>
compose(part="body",i=2,j=2,
as_paragraph("Page ",
as_word_field(x="Page"),
" of ",
as_word_field(x="NumPages"))) |>
width(width = c(4.5, 4.5))
doc = body_set_default_section(doc,
value = prop_section(page_size =
page_size(orient="landscape",width = 8.5,height=11),
header_default = block_list(my_header)))
print(doc, target = "test.docx")
This produces the following:
The problem is that when I open the document, I always get this message:
Is there a way to add the "Page X of N" without getting this message?
If you have Word installed, you can run/use package 'doconv'.
library(doconv)
print(doc, target = "test.docx") |> docx_update()
See https://github.com/ardata-fr/doconv#update-word-fields-and-toc