0

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

JdeMello
  • 1,708
  • 15
  • 23

1 Answers1

0

Per the documentation, DT only supports two DataTables plug-ins:

Currently these plug-ins have been integrated into DT:

  • Sorting
    • natural
  • Filtering
    • searchHighlight

It looks like sum() plug-in is not included in DT. The doucmentation states you can file a feature request using Github issues, if you want the sum() plug-in to be added.

Hallie Swan
  • 2,714
  • 1
  • 15
  • 23
  • Thanks. But isn't there a way to add up the plug-in to dataTables manually (i.e. using `` in the `` section? – JdeMello Mar 06 '18 at 17:52
  • Being more specific, by importing the plug-in code seeing [here](https://datatables.net/plug-ins/api/sum()) – JdeMello Mar 06 '18 at 17:58