如何使用 R 中的格式化包更改表上 N/A 的输出
How to change output for N/A on tables using formattable package in R
我正在使用格式table 包制作一个table,这里是table 和我的代码。
数据名 = 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 但不希望它是绿色或有 up/down 箭头。是否有可能变黑 and/or 有表示此数据不可用的横向箭头?
这是一个示例,其中 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
))
请注意,我按照您的预期在红色箭头向上、绿色向下箭头中离开了。
我正在使用格式table 包制作一个table,这里是table 和我的代码。
数据名 = 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"
这是我得到的输出
这是一个示例,其中 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
))
请注意,我按照您的预期在红色箭头向上、绿色向下箭头中离开了。