kableExtra:调整 add_header_above() 的字体和背景

kableExtra: adjust font face and background of add_header_above()

在用 kableExtra() 生成的 table 中,我想调整用 add_header_above() 添加的最上面两行的字体和背景。

下面提供的 MWE 允许对原始 table 的顶行进行调整。 不过,我的目标是

table 包含在 Rmarkdown 文档中,该文档同时编织到 PDF/LaTex 和 HTML。

MWE

mtcars[1:3,1:4] %>% kable() %>% 
    kable_styling(latex_options = c("striped", "scale_down")) %>% 
  # add column labels
  add_header_above(c(" " = 1, "Features" = 2, "Features" = 2)) %>% 
  add_header_above(c(" " = 1, "Properties A" = 2, "Properties B" = 2)) %>% 
  # adjust font face and backgroun
  row_spec(row = 0, italic = T) %>% 
  row_spec(row = 0, background = "orange") 

在为手动添加的 header/grouping 行添加 add_header_above() 的相关 post on SO on changing font size in kableExtra tables 提供答案时,我找到了我自己的问题的解决方案。

add_header_above() 实际上提供了很多很多参数来根据需要扭曲输出:

add_header_above(kable_input, header = NULL, bold = FALSE,
  italic = FALSE, monospace = FALSE, underline = FALSE,
  strikeout = FALSE, align = "c", color = NULL, background = NULL,
  font_size = NULL, angle = NULL, escape = TRUE, line = TRUE,
  line_sep = 3, extra_css = NULL, include_empty = FALSE)

说明性 MWE:

vec <- c("Properties A", "Properties B")
mtcars[1:3,1:4] %>% kable() %>% 
  kable_styling() %>% 
  # 2nd. level of grouping rows added on top of the table
  add_header_above(
    c(" " = 1, 
      "Features" = 2, 
      "Features" = 2), 
    font_size = 15, italic = TRUE) %>%
  # 1st. level of grouping rows added on top of the table (with dynamic labels as requested)
  add_header_above(
    c(" " = 1, 
      setNames(2, vec[1]),
      setNames(2, vec[1])), 
    font_size = 25, bold = TRUE, color = "orange", background = "lightblue") %>% 
  # adjust font face and background
  row_spec(row = 0, italic = T) %>% 
  row_spec(row = 0, background = "orange")