【发布时间】:2020-08-12 18:10:45
【问题描述】:
在 R markdown 中,我尝试使用 kable 包有条件地格式化 html 表。我有一个看起来像这样的表 bigT_output enter image description here
我使用 Kable 使用此代码对其进行格式化
kable(bigT_output[,1:7], booktabs=TRUE, align="c") %>%
pack_rows(
index = c("Performance Standard" = 1, "Percentile Rank (max 100)" = 3, "Productivity" = 2)
)%>%
kable_styling(bootstrap_options = "hover")
它在 html 输出中制作了这张漂亮的表格: enter image description here
理想情况下,我想格式化整个第一行(等级)以根据它是 ME、BE、DE 等来更改背景。但只是为了测试,我试图只更改一列的背景。当我使用 cell_spec 时,它会删除所有行标签并忽略我的其他 kable 格式选项,如下所示: enter image description here
代码如下:
bigT_output %>%
mutate(Merch = cell_spec(Merch, "html", background="green")) %>%
kable(bigT_output[,1:7], booktabs=TRUE, align="c") %>%
pack_rows(
index = c("Performance Standard" = 1, "Percentile Rank (max 100)" = 3, "Productivity" = 2)
)%>%
kable_styling(bootstrap_options = "hover")
如何在不丢失行标签的情况下同时使用副标题使我的表格美观并格式化它的某一行?我尝试的每件事都不断出错。感谢您的阅读。
【问题讨论】:
标签: r r-markdown kable kableextra