Table R 中带有 Kable 的电池条件

Table cell condition w/ Kable in R

我尝试为单元格 (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")

谢谢

这样试试:

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)