如何使用R

我正在使用formattable包制作一个表,这是我拥有的表和代码。

DataName = h1

如何使用R

customGreen0 = "#DeF7E9"
customGreen = "#71CA97"
customRed = "#ff7f7f"

improvement_formatter <- formatter("span",style = x ~ style(font.weight = "bold",color = ifelse(x > 0,customGreen,ifelse(x < 0,customRed,"black"))),x ~ icontext(ifelse(x<0,"arrow-up","arrow-down"),x)
)


formattable(h1,align =c("l","c","c"),list(
  `Totals` = formatter("span",style = ~ style(color = "grey",font.weight = "bold")),`2011`= color_tile(customGreen,customGreen0),`2012`= color_tile(customGreen,`2013`= color_tile(customGreen,`2014`= color_tile(customGreen,`2015`= color_tile(customGreen,`2016`= color_tile(customGreen,`2017`= color_tile(customGreen,`2018`= color_tile(customGreen,`2019`= color_tile(customGreen,`Average` = color_tile(customRed,customRed),`Change Since 2011` = improvement_formatter
))

h1[3,12] = "N/A"

这是我得到的输出

如何使用R

对于第3行,第12列,我需要将其设置为N / A,但不要将其设置为绿色或具有向上/向下箭头。是否可以变黑和/或使用向左箭头指示该数据不可用?

likui120 回答:如何使用R

在此示例中,NA没有任何颜色或箭头。在创建h1数据帧时,我使用了percent并将NA作为值之一。让我知道您是否打算这样做。

library(formattable)

h1 <- data.frame(
  Totals = c("a","b","c","d"),Y2011 = c(1230,779,37,1176),Average = c(830,347,25,1140),Change = percent(c(-.01,.67,NA,.02),digits = 0)
)

customGreen0 = "#DeF7E9"
customGreen = "#71CA97"
customRed = "#ff7f7f"

improvement_formatter <- formatter("span",style = x ~ style(font.weight = "bold",color = ifelse(x > 0,customGreen,ifelse(x < 0,customRed,"black"))),x ~ icontext(ifelse(x < 0,"arrow-up","arrow-down"),x)
)

formattable(h1,align =c("l","c"),list(
  `Totals` = formatter("span",style = ~ style(color = "grey",font.weight = "bold")),`Y2011`= color_tile(customGreen,customGreen0),`Average` = color_tile(customRed,customRed),`Change` = improvement_formatter
))

请注意,我按预期向左红色箭头,向绿色向下

formattable table

本文链接:https://www.f2er.com/2889560.html

大家都在问