繁体   English   中英

如何使用 R 中的 formattable 包更改表上 N/A 的输出

[英]How to change output for N/A on tables using formattable package in R

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

数据名称 = h1

数据图像

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","c","c", "c", "c", "c","c", "c","c", "c"), list(
  `Totals` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 
  `2011`= color_tile(customGreen, customGreen0),
  `2012`= color_tile(customGreen, customGreen0),
  `2013`= color_tile(customGreen, customGreen0),
  `2014`= color_tile(customGreen, customGreen0),
  `2015`= color_tile(customGreen, customGreen0),
  `2016`= color_tile(customGreen, customGreen0),
  `2017`= color_tile(customGreen, customGreen0),
  `2018`= color_tile(customGreen, customGreen0),
  `2019`= color_tile(customGreen, customGreen0),
  `Average` = color_tile(customRed, customRed),
  `Change Since 2011` = improvement_formatter
))

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

这是我得到的输出

在此处输入图片说明对于第 3 行第 12 列,我需要将其设为 N/A,但不希望它为绿色或带有向上/向下箭头。 是否有可能变黑和/或有侧向箭头表示此数据不可用?

这是一个示例,其中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", "c", "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
))

请注意,我按照您的意图以红色箭头向上,绿色向下。

可格式化表格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM