我试图改变文本的颜色,或者在我的桌子上的某些单元格中有颜色背景,使“正”绿色、“负”红色和“中性”保持黑色。
我已经搜索了一个解决方案,但是只能找到基于数字的条件格式。如何根据单元格中的文本更改颜色?
作为参考,这是我现在的代码:
`Date` <- c("24-02-2022", "04-04-2022", "01-04-2022", "18-05-2022", "28-02-2022", "04-03-2022", "05-03-2022", "08-03-2022", "05-05-2022", "12-05-2022", "25-05-2022")
`Predicted reaction` <- c("Negative", "Negative", "Negative", "Neutral", "Negative", "Positive", "Positive", "Negative", "Positive", "Negative", "Neutral")
df3 <- data_frame(`Date`, `Predicted reaction`)
df3 %>%
kbl(caption = "Timeline of Events") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
pack_rows("All", 1, 4) %>%
pack_rows("Spesific", 5, 11)这导致了下表:

发布于 2022-10-04 13:31:59
这就是你要找的吗?
为了节省一些输入量,我稍微修改了一些变量。
library(kableExtra)
library(dplyr)
date <- c("24-02-2022", "04-04-2022", "01-04-2022", "18-05-2022", "28-02-2022", "04-03-2022", "05-03-2022", "08-03-2022", "05-05-2022", "12-05-2022", "25-05-2022")
pr <- c("Negative", "Negative", "Negative", "Neutral", "Negative", "Positive", "Positive", "Negative", "Positive", "Negative", "Neutral")
df3 <-
data.frame(date, pr) |>
mutate(pr = cell_spec(pr, color = ifelse(pr == "Positive", "green", ifelse(pr == "Negative", "red", "black"))))
df3 |>
kable(caption = "Timeline of Events",
col.names = c("Date", "Predicted Reaction"),
escape = FALSE) |>
kable_classic(full_width = F, html_font = "Cambria")|>
pack_rows("All", 1, 4) |>
pack_rows("Specific", 5, 11)

https://stackoverflow.com/questions/73947086
复制相似问题