flextable 和 gtsummary:标题字体不同于 body 字体 save_as_docx()

flextable and gtsummary: Title font is different from body font with save_as_docx()

我正在尝试使用 gtsummaryflextable 将回归 tables 打印到 Microsoft Word 文件。然而,尽管尽可能指定了样式,table 的标题打印的字体与 table 的其余部分不同。我希望所有内容都采用 Times New Roman/APA 样式,但标题字体一直在 Cambria 中打印。在 R 之外,我的默认 Microsoft Word 字体是 Calibri。

我知道还有其他软件包可以将回归 tables 打印到 Microsoft Word,但我更喜欢 gtsummaryflextable,因为我的实际数据是多重估算的,我发现gtsummaryflextable 适用于乘法插补数据。这是一个小问题,但我们将不胜感激。

library(tidyverse)
library(gtsummary)
library(flextable)

packageVersion("gtsummary")
#> [1] '1.5.1'
packageVersion("flextable")
#> [1] '0.6.11.4'

# theme based on https://github.com/idea-labs/comsldpsy
apa_theme <- function (ft)  {
  ft %>%
    flextable::font(fontname = "Times New Roman", part = "all") %>% 
    flextable::fontsize(size = 12, part = "all") %>% 
    flextable::align(align = "left", part = "all") %>% 
    flextable::align(align = "center", part = "header") %>% 
    flextable::rotate(rotation = "lrtb", align = "top", part = "body") %>% 
    flextable::border_remove() %>% 
    flextable::hline_top(border = officer::fp_border(width = 2), part = "all") %>% 
    flextable::hline_bottom(border = officer::fp_border(width = 2), part = "all") %>% 
    flextable::autofit()
}

set_flextable_defaults(font.family = "Times New Roman")

m1 <- lm(response ~ trt, data = trial) %>% tbl_regression()
m2 <- lm(response ~ trt + marker, data = trial) %>% tbl_regression()
m3 <- lm(response ~ trt + marker + age, data = trial) %>% tbl_regression()

tbl_merge(
  tbls = list(m1, m2, m3)) %>%
  modify_table_styling(align = "left") %>%
  modify_caption("Why is the title in a different font?") %>% 
  as_flex_table() %>%
  apa_theme() %>%
  flextable::save_as_docx(path = "~/Desktop/weird_table.docx")

我能够通过使用 flextable::add_header_lines 而不是 gtsummary::modify_caption 并修改 apa_theme().

来达到预期的结果
library(tidyverse)
library(gtsummary)
library(flextable)

# theme based on https://github.com/idea-labs/comsldpsy
apa_theme <- function (ft)  {
  ft %>% 
    flextable::font(fontname = "Times New Roman", part = "all") %>% 
    flextable::fontsize(size = 12, part = "all") %>% 
    flextable::align(align = "left", part = "body") %>% 
    flextable::align(align = "center", part = "header") %>% 
    flextable::rotate(rotation = "lrtb", align = "top", part = "body") %>% 
    flextable::border_remove() %>% 
    flextable::hline_top(border = officer::fp_border(width = 2), 
                         part = "all") %>% 
    flextable::hline_bottom(border = officer::fp_border(width = 2),
                            part = "all") %>% 
    flextable::hline(i = 1, border = officer::fp_border(width = 1), part = "header") %>%
    flextable::set_table_properties(layout = "autofit")
}

m1 <- lm(response ~ trt, data = trial) %>% tbl_regression()
m2 <- lm(response ~ trt + marker, data = trial) %>% tbl_regression()
m3 <- lm(response ~ trt + marker + age, data = trial) %>% tbl_regression()

tbl_merge(
  tbls = list(m1, m2, m3)) %>%
  modify_table_styling(align = "left") %>%
  as_flex_table() %>%
  add_header_lines(values = "Table looks better overall", top = TRUE) %>%
  apa_theme() %>%
  flextable::save_as_docx(path = "~/Desktop/good_table.docx")