带有R的Kable的表格单元格条件

时间:2018-11-08 19:44:35

标签: r datatable kable kableextra

我尝试为单元格(data_a)与kable匹配的data_b着色,我在cell_spec中看到了ifelse函数,但是没有成功。

data_a <- data.table("01:05", "01:05", "01:16", "00:33", "00:52")
data_b <- data.table("00:02", "01:05", "02:30")

kable(data_a, row.names = FALSE, format = "html") %>%
      kable_styling(full_width = F, position = "center")

谢谢

1 个答案:

答案 0 :(得分:0)

像这样尝试:

library(data.table)
data_a <- data.table(a =c("01:05", "01:05", "01:16", "00:33", "00:52"))
data_b <- data.table(b =c("00:02", "01:05", "02:30"))
library(kableExtra)

data_a %>%
  mutate(a = cell_spec(a, "html", color = ifelse(a %in% data_b$b, "green", "red"))) %>%
  kable(format = "html", escape = F) %>%
  kable_styling("striped", full_width = F)

enter image description here