0

In a table generated with kableExtra() I would like to adjust the font face and background of the two top rows added with add_header_above().

The MWE provided below allows making adjustments to the top row of the original table. However, my goal is that

  • the upper top row added has a bold font face and a colored background, and
  • the lower top row added has an italic font face.

The table is included in a Rmarkdown document which is knitted both to PDF/LaTex and 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") 

enter image description here

mavericks
  • 1,005
  • 17
  • 42

1 Answers1

1

While providing an answer for this related post on SO on changing font size in kableExtra tables for manually added header/grouping rows added with add_header_above(), I came across the solution to my own question.

add_header_above() actually provides many, many arguments to twist the output as desired:

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)

An illustrative 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")

enter image description here

mavericks
  • 1,005
  • 17
  • 42