条件格式化表格的背景颜色(R中的formattable)

时间:2017-09-18 22:29:33

标签: r formattable

我有一张类似下面的表格:

library(tibble)
library(formattable)

mytable <- tibble(
id = c(NA, 748, 17, 717, 39, 734, 10, 762),
NPS = c(65, 63, 56, 62, 73, 80, 50, 54),
`NPS Chge vs. month ago` = c(-2, -5, -2, -8, -1, 6, 7, -9),
`Cumulative Response` = c(766, 102, 154, 81, 239, 79, 50, 61),
`Response Rate` = c(0.25, 0.24, 0.25, 0.34, 0.21, 0.34, 0.32, 0.27),
`Response for Month` = c(161, 43, 7, 37, 7, 32, 15, 20)
)

formattable(mytable)

enter image description here

我希望将条件格式设置为行的背景,这样如果NPS分数低于60,则背景设置为红色,否则设置为绿色。由于我对HTML的了解有限,我想我可以使用&#34; td&#34;。不幸的是,它似乎使整个表的格式变得混乱:

html_tag <- "td"
my_format <- formatter(html_tag, style = x ~ ifelse(mytable$NPS < 60, "background-color:red", "background-color:green"))

formattable(mytable, list(
  area(col = 2:6) ~ my_format
))

enter image description here

表格的标题不再与其余行对齐。我究竟做错了什么?我该怎么用而不是&#34; td&#34;?

0 个答案:

没有答案