我正在尝试在我闪亮的表格输出中使用一千个分隔符。以下是我的代码:
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 ))问题出在可格式化部分不起作用。我不知道如何用formattable正确地输入函数comma()。
发布于 2021-01-29 01:42:58
有两种方法可以完成此操作:
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)
)
}发布于 2019-06-12 15:29:55
如果您只是尝试将数千个标记添加到您的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=",")https://stackoverflow.com/questions/56556329
复制相似问题