[英]Use comma function in formattable (R)
I'm trying to have thousand separator in my shiny table output.我正在尝试在闪亮的表输出中使用千位分隔符。 The following are my code:以下是我的代码:
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 = ","
))
})
}
My df's are varied but all of them are in the format:我的 df 是多种多样的,但它们都采用以下格式:
data.frame("Channel" = c("A", "B", "C", "D"),
"Amount" = c(1000000, 2000000, 3000000, 4000000 ))
The problem is with the formattable part that it didn't work.问题在于它不起作用的可格式化部分。 I don't know how to input the function comma()
properly with formattable
.我不知道如何使用formattable
正确输入函数comma()
。
There are two ways to do this:有两种方法可以做到这一点:
1. Create a Custom Function 1. 创建自定义函数
mycomma <- function(digits = 0) {
formatter("span", x ~ comma(x, digits = digits)
)
}
Use for a single named column:用于单个命名列:
formattable(df, list(`Account` = mycomma(digits=0))
)
Use for several columns:用于多列:
formattable(df,
list(area(col = c(3:6)) ~ mycomma(digits=0))
)
2. pass the formattable function each time 2.每次都传递formattable函数
formattable(df, list(`Account` = formatter("span", x ~ comma(x, digits = 0))
)
NOTE: You could just as easily create a custom function that could be passed a different function, like percent or accounting.注意:您可以轻松创建自定义函数,该函数可以传递不同的函数,例如百分比或会计。 YMMV青年会
mysimpleformat <- function(fun = "comma", digits = 0) {
fun <- match.fun(fun)
formatter("span", x ~ fun(x, digits = digits)
)
}
If you're just trying to add the thousand markers to your Amount
column, you can do it as follows:如果您只是想将千位标记添加到您的Amount
列,您可以按如下方式进行:
df <- data.frame("Channel" = c("A", "B", "C", "D"),
"Amount" = c(1000000, 2000000, 3000000, 4000000 ))
df$Amount <- format(as.integer(df$Amount), big.mark=",")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.