如何在带有彩色条的格式表中将数字显示为美元金额

问题描述 投票:2回答:1

因此,我试图制作一个很酷的表,其中一个数值将显示为彩色条,而数字则作为格式表中的文本显示。我的销售编号是整数,我知道我可以使用currency()函数将它们更改为货币格式,而不会丢失它们作为数值。但是,一旦我添加了normalize_bar功能,数字便不再显示为货币格式。有人可以告诉我如何正确格式化吗?

prod$Sales<-currency(prod$Sales,digits=0L)
table<-formattable(prod, list(
area(col=Sales)~normalize_bar("green",0.2)
))

结果看起来像这样。

Table with bars

尽管我知道销售编号的格式已更改为货币(请参见下面无区域的表格)。但是如何使数字正确显示呢?Table without bars

r format currency formattable
1个回答
0
投票

您可以编写自己的函数来格式化可格式化的输出。在这里,我展示了一个示例,其中默认输出是带有和不带有标准化条的货币。

currencyout <- function(fun="currency"){
  fun <- match.fun(fun)
  formatter("span", x ~ fun(x, digits = 2))
}
currencybarout <- function(color = "violet", fun = "currency") {
  fun <- match.fun(fun)
  formatter("span", x ~ fun(x, digits = 2),
            style = function(y) style(
              display = "inline-block",
              direction = "rtl",
              "border-radius" = "4px",
              "padding-right" = "2px",
              "background-color" = csscolor(color),
              width = percent(proportion(as.numeric(y), na.rm = TRUE))
            )
  )
}

例如,您可以在第一个列之后的所有列的数据帧df中实现这一点(我假设是描述)

formattable(df,
            list(area(col = 2:ncol(df)) ~ currencyout())
                 )
formattable(df,
            list(area(col = 2:ncol(df)) ~ currencybarout())
                 )
© www.soinside.com 2019 - 2024. All rights reserved.