更改闪亮数据表的列总和输出
Change column sum output of shiny datatable
这是此问题的跟进问题:Adding Total/Subtotal to the bottom of a DataTable in Shiny,代码段如下:
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
h1('Testing TableTools'),
mainPanel(
dataTableOutput('display')
)
))
Names <- c("",names(mtcars))
FooterNames <- c(rep("",4),Names[5:6],rep("",6))
server <- function(input, output, session) {
sketch <- htmltools::withTags(table(
tableHeader(Names),tableFooter(FooterNames)
))
opts <- list(
dom = 'Bfrtip', buttons = list('colvis','print',list(extend='collection',text='Download',buttons = list('copy','csv','excel','pdf'))),
footerCallback = JS(
"function( tfoot, data, start, end, display ) {",
"var api = this.api(), data;",
"$( api.column(5).footer()).html('SubTotal: '+",
"api.column(5).data().reduce( function ( a, b ) {",
"return a + b;",
"} )",
");",
"$( api.column(4).footer()).html('SubTotal: '+",
"api.column(4).data().reduce( function ( a, b ) {",
"return a + b;",
"} )",
");","}")
)
output$display <- DT::renderDataTable(container = sketch,extensions = 'Buttons',options = opts,{
mtcars
})
}
shinyApp(ui = ui, server = server)
在我的数据框中,我的列包含逗号后有两位数字的双精度数。小计显示如下:
1234,51999999999 而不是 1234,52。
我必须在 Javascript 代码中更改什么才能在逗号后仅获得两位数字,百分号分隔符作为点,小数点作为逗号,接下来是欧元 (€) 符号总和?
如果您使用的是 shiny,为什么要在 JavaScript 中执行此操作?这是 R 方式:
paste0(
formatC(1234.51999999999, format="f", big.mark=".",
decimal.mark = ",", digits=2), "€"
)
# [1] "1.234,52€"
或者用JS来完成:
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
h1("Testing TableTools"),
mainPanel(
dataTableOutput("display")
)
))
Names <- c("", names(mtcars))
FooterNames <- c(rep("", 4), Names[5:6], rep("", 6))
server <- function(input, output, session) {
sketch <- htmltools::withTags(table(
tableHeader(Names), tableFooter(FooterNames)
))
opts <- list(
dom = "Bfrtip", buttons = list("colvis", "print", list(extend = "collection", text = "Download", buttons = list("copy", "csv", "excel", "pdf"))),
footerCallback = JS(
"
function(tfoot, data, start, end, display) {
var api = this.api(),
data;
var sum1 = api.column(5).data().reduce(function(a, b) {
return a + b;
});
sum1 = Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(sum1)
$(api.column(5).footer()).html('SubTotal: ' + sum1)
}
"
)
)
output$display <- DT::renderDataTable(container = sketch, extensions = "Buttons", options = opts, {
mtcars
})
}
shinyApp(ui = ui, server = server)
这是此问题的跟进问题:Adding Total/Subtotal to the bottom of a DataTable in Shiny,代码段如下:
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
h1('Testing TableTools'),
mainPanel(
dataTableOutput('display')
)
))
Names <- c("",names(mtcars))
FooterNames <- c(rep("",4),Names[5:6],rep("",6))
server <- function(input, output, session) {
sketch <- htmltools::withTags(table(
tableHeader(Names),tableFooter(FooterNames)
))
opts <- list(
dom = 'Bfrtip', buttons = list('colvis','print',list(extend='collection',text='Download',buttons = list('copy','csv','excel','pdf'))),
footerCallback = JS(
"function( tfoot, data, start, end, display ) {",
"var api = this.api(), data;",
"$( api.column(5).footer()).html('SubTotal: '+",
"api.column(5).data().reduce( function ( a, b ) {",
"return a + b;",
"} )",
");",
"$( api.column(4).footer()).html('SubTotal: '+",
"api.column(4).data().reduce( function ( a, b ) {",
"return a + b;",
"} )",
");","}")
)
output$display <- DT::renderDataTable(container = sketch,extensions = 'Buttons',options = opts,{
mtcars
})
}
shinyApp(ui = ui, server = server)
在我的数据框中,我的列包含逗号后有两位数字的双精度数。小计显示如下:
1234,51999999999 而不是 1234,52。
我必须在 Javascript 代码中更改什么才能在逗号后仅获得两位数字,百分号分隔符作为点,小数点作为逗号,接下来是欧元 (€) 符号总和?
如果您使用的是 shiny,为什么要在 JavaScript 中执行此操作?这是 R 方式:
paste0(
formatC(1234.51999999999, format="f", big.mark=".",
decimal.mark = ",", digits=2), "€"
)
# [1] "1.234,52€"
或者用JS来完成:
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
h1("Testing TableTools"),
mainPanel(
dataTableOutput("display")
)
))
Names <- c("", names(mtcars))
FooterNames <- c(rep("", 4), Names[5:6], rep("", 6))
server <- function(input, output, session) {
sketch <- htmltools::withTags(table(
tableHeader(Names), tableFooter(FooterNames)
))
opts <- list(
dom = "Bfrtip", buttons = list("colvis", "print", list(extend = "collection", text = "Download", buttons = list("copy", "csv", "excel", "pdf"))),
footerCallback = JS(
"
function(tfoot, data, start, end, display) {
var api = this.api(),
data;
var sum1 = api.column(5).data().reduce(function(a, b) {
return a + b;
});
sum1 = Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(sum1)
$(api.column(5).footer()).html('SubTotal: ' + sum1)
}
"
)
)
output$display <- DT::renderDataTable(container = sketch, extensions = "Buttons", options = opts, {
mtcars
})
}
shinyApp(ui = ui, server = server)