How could one add a caption to a flextable rendered to docx? EDIT: The aim is to produce a proper caption which can be referenced within the document to produce a list of tables and inline references.
iris.t <-
iris[1:5,] %>%
regulartable() %>%
style(pr_c = officer::fp_cell(vertical.align = "bottom",
border.bottom = officer::fp_border(width = 2)), part = "header") %>%
rotate(j = names(iris)[-c(1:2)],
rotation = "tbrl", part = "header", align = "bottom") %>%
height(height = max(dim_pretty(., part = "header")$widths), part = "header") %>%
width(width = dim_pretty(.,part = "body")$widths)
iris.t
I use the following way to caption tables.
#set the table caption styling
knitr::opts_chunk$set(tab.cap.pre = "Table ", tab.cap.sep = ": ")
#set the table caption styling
set_flextable_defaults(font.family = "Calibri (Body)",
font.size = 9,
digits = 0,
border.color = "#000000",
padding.bottom = 1,
padding.top = 1,
padding.left = 3,
padding.right = 1)
ft <- flextable(df, defaults = TRUE) #convert to flextable object
autonum <- run_autonum(seq_id = "tab", bkm = "TC1", bkm_all = TRUE) # number the table, bkm (bookmark) is important as the cross-referencing is done using the bookmark
ft <- set_caption(ft, caption = "Traffic Counts for Existing Condition",
style = "Table Caption", autonum = autonum)
ft # to print the table
to cross-reference the table use \@ref(tab:TC1)
to produce a list of tables use
<!---BLOCK_TOC{seq_id: 'tab'}--->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With