【问题标题】:kableExtra: adjust font face and background of add_header_above()kableExtra:调整 add_header_above() 的字体和背景
【发布时间】:2020-06-30 13:50:56
【问题描述】:

在使用kableExtra() 生成的表格中,我想调整使用add_header_above() 添加的顶部两行的字体和背景。

下面提供的 MWE 允许对原始表格的第一行进行调整。 但是,我的目标是

  • 添加的顶行具有粗体字体和彩色背景,并且
  • 添加的顶下行有一个斜体字体。

该表格包含在 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") 

【问题讨论】:

    标签: r r-markdown kable kableextra


    【解决方案1】:

    在为使用 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")
    

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2020-01-23
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2010-09-16
      相关资源
      最近更新 更多