I am trying to use the API's datatTables sum()
plug-in in my app without success. The following app works:
library(shiny)
library(DT)
set.seed(2282018)
company <- data.frame(Company = letters[1:10], Units = round(runif(10,
1000, 10e6), 0),
Price = scales::dollar(runif(10, 200, 1230)), stringsAsFactors = F)
jsCode <- "function(row, data, start, end, display) {
var api = this.api(), data;
total = api.column(1).data().reduce( function(a, b) {return a + b}, 0);
$( api.column(1).footer() ).html('Total: ' + total);
}"
# UI ----
ui <- function(){
fluidPage(
sidebarLayout(
sidebarPanel(numericInput("nums", label = "Num Input", value = 1, min = 1, max = 10)),
mainPanel(dataTableOutput("mytable"))
)
)
}
# server ----
server <- function(input, output, session){
cont <- htmltools::withTags(table(
tableHeader(names(company)),tableFooter(names(company))
))
output$mytable <- DT::renderDataTable( {
DT::datatable(company,
container = cont,
caption = tags$caption("Example"),
filter = "none",
rownames = F,
options = list(autoWidth = T,
pageLength = 10,
scrollCollapse = T,
dom = 'lftp',
footerCallback = JS(jsCode))
)
}
)
}
runApp(list(ui = ui, server = server))
Note that the js code object jsCode
works but I would like to rewrite it with:
jsCode <- "function(row, data, start, end, display) {
var api = this.api(), data;
// change below
total = api.column(1).data().sum();
$( api.column(1).footer() ).html('Total: ' + total);
}"
This code should also work if I understand the documentation correctly but I am failing to upload the plugin.
Thank you