簡體   English   中英

創建具有全彩色背景的kable表

[英]Create kable table with fully-colored background

我在R創建一個格式很好的表時遇到了麻煩。 我90%的路在那里,但不能一路走來。

我需要使用背景顏色為整個單元格着色,如下例所示。 我閱讀了kable插圖並看到以html格式, background不會為整個單元格着色。 有辦法解決這個問題嗎? 我嘗試將它設置為乳膠,但輸出是乳膠而不是在查看器中顯示。 我也是一個新手markdown用戶,所以當我在那里嘗試時,輸出不是我希望的(這只是一個自包含的表)。

我已經在SO上搜索了大量的解決方案,但我無法得到它。 R制作表格肯定不容易。 任何幫助,將不勝感激。

樣本數據:

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')

為了成功,我們需要創建表對象( 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寬。

結果:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM