r - 在 formattable (R) 中使用逗号函数
问题描述
我正在尝试在闪亮的表格输出中使用千位分隔符。以下是我的代码:
UI <- dashboardPage(
dashboardBody(
fluidPage(
selectInput(inputId = "TableName",
label = "Select Data of Interest",
choices = list.files(path = "CSVData", pattern = "AMD_")),
uiOutput("Channel"),
formattableOutput("ResultTable")
)
)
)
Server <- function(input, output){
output$Channel <- renderUI({
df <- read.csv(paste("Pathname/",input$TableName, sep = ""))
selectInput(inputId = "ChannelSelect",
label = "Select Channel:",
choices = unique(df) %>% select(Channel))),
selected = NULL, multiple = FALSE)
})
output$ResultTable <- renderFormattable({
df <- read.csv(paste("CSVData/",input$TableName, sep = ""))
ChanSelect <- input$ChannelSelect
A1 <- df %>% filter(if(ChanSelect != "All") (Channel == ChanSelect) else TRUE) %>%
select(Channel, Amount)
formattable(A1, list(
`Amount` = comma(`Amount`, digits = 0, format = "f", big.mark = ","
))
})
}
我的 df 多种多样,但都采用以下格式:
data.frame("Channel" = c("A", "B", "C", "D"),
"Amount" = c(1000000, 2000000, 3000000, 4000000 ))
问题在于它不起作用的可格式化部分。我不知道如何comma()
正确输入函数formattable
。
解决方案
有两种方法可以做到这一点:
1. 创建自定义函数
mycomma <- function(digits = 0) {
formatter("span", x ~ comma(x, digits = digits)
)
}
用于单个命名列:
formattable(df, list(`Account` = mycomma(digits=0))
)
用于多列:
formattable(df,
list(area(col = c(3:6)) ~ mycomma(digits=0))
)
2.每次通过formattable函数
formattable(df, list(`Account` = formatter("span", x ~ comma(x, digits = 0))
)
注意:您可以轻松地创建一个自定义函数,该函数可以传递不同的函数,例如百分比或会计。YMMV
mysimpleformat <- function(fun = "comma", digits = 0) {
fun <- match.fun(fun)
formatter("span", x ~ fun(x, digits = digits)
)
}
推荐阅读
- bigtable - 如何在 Bigtable 中重命名表?
- javascript - 在 jasmine 中测试 Observables
- html - 试图将弹性项目与底部对齐,但它们却与第一行文本对齐
- angularjs - 使用样式格式从 HTML 导出 XLS 时出现 AlaSql 错误
- c# - Json 从数组 C# Xamarin 中的数组中获取项目
- laravel - Laravel 背包。在具有多个前缀的 crud 列表中禁止出现错误 403
- php - 如何纠正mysql php更新中的错误?
- python - 如何使用 Canvas.create_image
- regex - 正则表达式(三个字符,然后是 3-7 位数字),然后是可选的一个字符
- php - 为什么 undefined 没有被删除