add a flextable into a Word document.
body_add_flextable(
x,
value,
align = "center",
pos = "after",
split = FALSE,
topcaption = TRUE,
keepnext = TRUE
)
body_replace_flextable_at_bkm(
x,
bookmark,
value,
align = "center",
split = FALSE
)
an rdocx object
flextable
object
left, center (default) or right.
where to add the flextable relative to the cursor, one of "after", "before", "on" (end of line).
set to TRUE if you want to activate Word option 'Allow row to break across pages'.
if TRUE caption is added before the table, if FALSE, caption is added after the table.
default TRUE
. Word option 'keep rows
together' is activated when TRUE. It avoids page break
within tables. This is handy for small tables, i.e. less than
a page height.
Be careful, if you print long tables, you should
rather set its value to FALSE
to avoid that the tables
also generate a page break before being placed in the
Word document. Since Word will try to keep it with the next
paragraphs that follow the tables.
bookmark id
Use this function if you want to replace a paragraph containing a bookmark with a flextable. As a side effect, the bookmark will be lost.
library(officer)
# autonum for caption
autonum <- run_autonum(seq_id = "tab", bkm = "mtcars")
ftab <- flextable( head( mtcars ) )
ftab <- set_caption(ftab, caption = "mtcars data", autonum = autonum)
ftab <- autofit(ftab)
doc <- read_docx()
doc <- body_add_flextable(doc, value = ftab)
fileout <- tempfile(fileext = ".docx")
# fileout <- "test.docx" # uncomment to write in your working directory
print(doc, target = fileout)