如何使用kableExtra为一个单元着色

时间:2019-06-05 15:44:37

标签: r kableextra

我只是想用kableExtra突出显示表格中的一个单元格。我遇到的问题是我的某些单元格带有$ s和()s。这是它的样子

df3 <- data.frame(
  "Bitcoin Price:" = c("Snow Panther B1+", "ASICminer 8 nano", "S9", "Avalon 921", "Dragonmint T1", "Edit E11++"), 
  "3000" = c("($270.71)", "($3376.85)", "($115.80)", "($530.81)", "($1108.14)", "($1035.42)"),
  "6000" = c("$1050.37", "($1004.31)", "$666.06", "$547.62", "($245.39)", "$1337.12"), 
  "9000" = c("$2371.44", "$1368.24", "$1447.92", "$1626.04", "$617.35", "$3709.66"),
  stringsAsFactors = FALSE, check.names=FALSE)

我已经尝试过了,但是不起作用

df3 %>%
  mutate(
    `6000`[,2] = cell_spec(`6000`[,2], color = "red", bold = T)
  ) %>%
  select("Bitcoin Price:", everything()) %>%
  kable(align = "c", escape = F) %>%
  kable_styling("hover", "striped", full_width = F)  %>%
  add_header_above(c(" " = 1, "Current Difficulty" = 3)) %>%
  add_footnote(c("Statistics Calculated 2019"), notation = "symbol")

有人有什么建议吗?我觉得我已经接近了。我正在尝试将值($ 1004.31)的单元格设置为红色。

1 个答案:

答案 0 :(得分:1)

这是您要寻找的吗?

df3 %>%
  mutate(`6000` = cell_spec(`6000`, "html",color = ifelse(`6000` == "($1004.31)", "red", "grey"))) %>%
  select("Bitcoin Price:", everything()) %>%
  kable(align = "c", escape = F) %>%
  kable_styling("hover", "striped", full_width = F)  %>%
  add_header_above(c(" " = 1, "Current Difficulty" = 3)) %>%
  add_footnote(c("Statistics Calculated 2019"), notation = "symbol")