# \dontshow{
init_flextable_defaults()
# }
library(officer)
# define global settings
set_flextable_defaults(
split = TRUE,
table_align = "center",
table.layout = "autofit"
)
# produce 3 flextable
ft_1 <- flextable(head(airquality, n = 20))
ft_1 <- color(ft_1, i = ~ Temp > 70, color = "red", j = "Temp")
ft_1 <- highlight(ft_1, i = ~ Wind < 8, color = "yellow", j = "Wind")
ft_1 <- set_caption(
x = ft_1,
autonum = run_autonum(seq_id = "tab"),
caption = "Daily air quality measurements"
)
ft_1 <- paginate(ft_1, init = TRUE, hdr_ftr = TRUE)
ft_2 <- proc_freq(mtcars, "vs", "gear")
ft_2 <- set_caption(
x = ft_2,
autonum = run_autonum(seq_id = "tab", bkm = "mtcars"),
caption = as_paragraph(
as_b("mtcars"), " ",
colorize("table", color = "orange")
),
fp_p = fp_par(keep_with_next = TRUE)
)
ft_2 <- paginate(ft_2, init = TRUE, hdr_ftr = TRUE)
ft_3 <- summarizor(iris, by = "Species")
ft_3 <- as_flextable(ft_3, spread_first_col = TRUE)
ft_3 <- set_caption(
x = ft_3,
autonum = run_autonum(seq_id = "tab"),
caption = "iris summary"
)
ft_3 <- paginate(ft_3, init = TRUE, hdr_ftr = TRUE)
# add the 3 flextable in a new Word document
doc <- read_docx()
doc <- body_add_flextable(doc, value = ft_1)
doc <- body_add_par(doc, value = "")
doc <- body_add_flextable(doc, value = ft_2)
doc <- body_add_par(doc, value = "")
doc <- body_add_flextable(doc, value = ft_3)
fileout <- tempfile(fileext = ".docx")
print(doc, target = fileout)
# \dontshow{
init_flextable_defaults()
# }
Run the code above in your browser using DataCamp Workspace