有没有办法在使用 R 的 flextable 包时将传递给 add_header_lines() 的字符串的一部分加粗

Is there a way to bold part of a character string being passed to add_header_lines() when using the flextable package for R

我正在使用我喜欢的 flextable 包为 Word 文档创建几个 table。但是,我在将 table 标题中的部分文本加粗时遇到了一些麻烦。例如,我希望标题显示为“Table 1. 我的 table 标题的其余部分。”而不是“Table 1. 我的 table 标题的其余部分。”

found this documentation,经过反复试验,我终于得到了我想要的结果。但感觉可能有更直接的方法来获取它。这是我目前的解决方案:

library(dplyr)
library(flextable)

mtcars_ft <- flextable(head(mtcars)) %>% 
  # Add a blank title line to top of table
  add_header_lines("") %>% 
  # Use compose to bold "Table #."
  compose(
    i = 1, part = "header",
    value = as_paragraph(
      as_chunk("Table 1. ", props = fp_text(bold = TRUE)),
      "Here is my example mtcars ft."
    ),
  )

这是我的结果截图:

下面是一个解决方案,既可以做你想做的事,也可以在 Word 中使用 auto-numbering,这样如果更新,数字和引用也会更新。

library(officer)
library(flextable)

mtcars_ft <- flextable(head(mtcars)) %>% set_table_properties(layout = "autofit")

bold_face <- fp_text(bold = TRUE, font.size = 11)

fpar_ <- fpar(
  run_autonum(seq_id = 'tab', 
              bkm = 'a_bkm', pre_label = "Table ", 
              prop = bold_face),
              "Here is my example mtcars ft." )

read_docx() %>% 
  body_add_fpar(fpar_, style = "centered") %>% 
  body_add_flextable(mtcars_ft) %>% 
  print(target = "example.docx")

这基本上就是 flextable::set_caption 所做的(但无法根据需要格式化文本)