簡體   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