I'm trying to switch from ReporteRs
to officer
. When using ReporteRs
, I was able to add a FlexTable to the header by using a bookmark that I set up in a Word document template (I placed a bookmark in the header of the template called "HEAD"):
library(ReporteRs)
library(dplyr)
doc <- docx(template = "Template.docx")
ft1 <- FlexTable(mtcars)
addFlexTable(doc,
ft1,
bookmark = "HEAD")
writeDoc(doc, file = "test.docx")
Presumably, it might be possible do the same thing using officer
with the flextable
package, and it might look something like this:
library(officer)
library(flextable)
library(dplyr)
doc <- docx(template = "Template.docx")
ft1 <- flextable(mtcars)
doc <- cursor_bookmark(doc,"HEAD") %>% body_add_flextable(ft)
print(doc,target="test.docx")
If I have a bookmark named "HEAD" in the body of the document, this works, but if I have a bookmark named "HEAD" in the header of the Word document, it says Error: cannot find bookmark "HEAD"
.
I know there are specific functions to add text to the header, but these don't have the ability to add tables. The closest I can find is:
doc <- docx(template = "Template.docx")
ft1 <- flextable(mtcars)
doc <- headers_replace_text_at_bkm(doc,"HEAD",ft)
print(doc,target="test.docx")
But this returns the error Error in headers_replace_text_at_bkm(doc, "HEAD", ft) : is_scalar_character(value) is not TRUE
.
Is there any way to add a table to the header using officer
and flextable
?