For further clarification, this question is an extension of a previous inquiry, which can be viewed here.
In the following code snippet, I am calculating the column sum of a Shiny datatable using Javascript in order to display it directly below the table.
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
h1("Testing TableTools"),
mainPanel(dataTableOutput("display"))
))
Names <- c("", names(mtcars))
FooterNames <- c(rep("", 5), Names[6], rep("", 6))
server <- function(input, output, session) {
sketch <- htmltools::withTags(table(
tableHeader(Names), tableFooter(FooterNames)
))
opts <- list(
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)
https://i.sstatic.net/eE48s.png
One point to note is that sorting the table will alter the column sum, as the total only represents the displayed column. My goal is to modify this so that it reflects the overall sum when dealing with multiple "pages" of the table, while still maintaining the current functionality for single-page scenarios.