在docx中为flextable添加标题

时间:2018-04-05 16:13:48

标签: r r-markdown knitr flextable

如何为向docx呈现的flextable添加标题? 编辑:目的是产生一个适当的标题,可以在文档中引用,以生成表格和内联参考文献列表。

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 

1 个答案:

答案 0 :(得分:1)

我使用以下方式为表格添加标题。

#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

交叉引用表格使用\@ref(tab:TC1)

生成表格列表使用

<!---BLOCK_TOC{seq_id: 'tab'}--->