library(flextable)
# define some default values ----
set_flextable_defaults(font.size = 22, border.color = "gray")
# an example with append_chunks ----
pp_docx = function(x) {
x <- add_header_lines(x, "Page ")
x <- append_chunks(
x = x, i = 1, part = "header", j = 1,
as_word_field(x = "Page")
)
align(x, part = "header", align = "left")
}
ft_1 <- flextable(cars)
ft_1 <- autofit(ft_1)
ft_1 <- pp_docx(ft_1)
## or:
# set_flextable_defaults(post_process_docx = pp_docx)
## to prevent this line addition when output is not docx
# print(ft_1, preview = "docx")
# an example with compose ----
library(officer)
ft_2 <- flextable(head(cars))
ft_2 <- add_footer_lines(ft_2, "temp text")
ft_2 <- compose(
x = ft_2, part = "footer", i = 1, j = 1,
as_paragraph("p. ",
as_word_field(x = "Page", width = .05),
" on ", as_word_field(x = "NumPages", width = .05))
)
ft_2 <- autofit(ft_2, part = c("header", "body"))
doc <- read_docx()
doc <- body_add_flextable(doc, ft_2)
doc <- body_add_break(doc)
doc <- body_add_flextable(doc, ft_2)
outfile <- print(doc, target = tempfile(fileext = ".docx"))
# reset default values ----
init_flextable_defaults()
Run the code above in your browser using DataLab