创建具有全彩色背景的 kable table

Create kable table with fully-colored background

我在 R 中创建格式良好的 table 时遇到问题。我已经完成了 90% 的进度,但还没有完全完成。

我需要用背景色为整个单元格着色,如下例所示。我阅读了 kable 小插图,发现在 html 格式中,background 不会为整个单元格着色。有办法解决这个问题吗?我尝试将其设置为乳胶,但输出是乳胶而不是在查看器中显示。我也是新手 markdown 用户,所以当我在那里尝试时,输出不是我所希望的(这只是一个独立的 table)。

我在 SO 上进行了大量搜索以寻找解决方案,但我一直无法找到它。在 R 中产生 table 确实不容易。任何帮助将不胜感激。

示例数据:

library(tidyverse)
df <- structure(list(Indicator = c("Var1", "Var2", "Var3", "Var4", "Var5"
), Sign = c(-1L, 1L, 1L, -1L, 1L), Freq = c("M", "A", "Q", "M", 
                                            "M")), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
df
# A tibble: 5 x 3
  Indicator  Sign Freq 
  <chr>     <int> <chr>
1 Var1         -1 M    
2 Var2          1 A    
3 Var3          1 Q    
4 Var4         -1 M    
5 Var5          1 M 

尝试的代码:

library(kable)
library(kableExtra)
df  %>% 
      dplyr::rename(Trend = Freq) %>%
      mutate(Indicator = cell_spec(Indicator, "html", color = "black", bold = T), 
             Trend = cell_spec(Trend, "html", color = "white", bold = T, 
                               background = factor(Sign, c(-1, 0, 1), 
                                                   c("red", "gray", "green")))) %>%
      select(Indicator, Trend) %>%
      kable(align = c('l', 'c'), format = "html", escape = F) %>%
      kable_styling(bootstrap_options = c("bordered", full_width = F, font_size = 16)) %>% 
      row_spec(0, background = "rgb(172, 178, 152)", color = "black", font_size = 18)

我简化了初始数据以使其清晰:

df <- tribble(~Indicator, ~Freq, ~cellColor,
              'Speed', 43.342, 'red',
              'Altitude', 44.444, 'blue',
              'Smartness', 0.343, 'green')

要成功,我们需要创建 table 对象 (tbl),因为 kable 库具有用于固定列宽设置的函数 column_spec

tbl <- df %>% 
  mutate(Indicator = cell_spec(Indicator, "html", color = "black", bold = T), 
         Freq = cell_spec(x = Freq, 
                           format = "html", 
                           color = "white", 
                           bold = T, 
                           extra_css = paste(paste('background-color', cellColor, sep = ': '), # combine background-color CSS rule with the observation vector value
                                             'display: inline-block', # extremely important CSS modifier for the span tag in the table cell
                                             'text-align: center', # text align
                                             'padding: 0px', # expand the field of text
                                             'margin: 0px', # expand the field of text
                                             'width: 200px', # future cell/column width
                                             sep = "; "), # CSS notation rule
                           )
  ) %>%
  select(-cellColor) %>% # exclude cellColor vector
  kable(format = "html", escape = F) %>%
  kable_styling(bootstrap_options = c("bordered", full_width = F, font_size = 16))

column_spec(tbl, 2, width = "200px") # set the column width as the cell width

tbl # print

如您所见,匹配列和单元格大小很重要。例如,我将它们都设置为 200px 宽。

结果: