在Rmarkdown中使用Kable对特定单元格进行着色

tjrkku2a  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(123)

我有这个数据框

我怎么能做这样的东西

有凯布尔套餐吗

g0czyy6m

g0czyy6m1#

你可以在kableExtra库中使用cell_spec()函数,也可以在Dplyr包中使用mutate()。
它看起来像这样:

table <- df %>%  
  mutate(Column = cell_spec(Column, "html", background_as_tile = T, background = factor(Column, colours))) %>%
  kable()...
rdlzhqv9

rdlzhqv92#

首先定义dataframe。

df = data.frame(c1 = c("0-4","5-14","15-24","25-34"),c2 = c(413,48,155,175),
                    c3 = c(428,33,125,180),c4 = c(345,44,397,642))

这里的命令为简单的表格,不带颜色.

df |> knitr::kable(format = "html",escape = FALSE, align = 'c',col.names = c("Grupo","1990","2000","2010"))

现在,使用kableExtra包,我们可以编辑表。

library(kableExtra)
df[1,2] = cell_spec(df[1,2], background = "forestgreen",format = "html")  
df[3,3] = cell_spec(df[3,3], background = "orange",format = "html")  

df |> knitr::kable(format = "html",escape = FALSE, align = 'c',col.names = c("Grupo","1990","2000","2010"))

相关问题