Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Add totals to a DT::datatable?

I am trying to add totals to a data table footer. Using code from different sources, I wrote the following application using Shiny. The problem is, when I run it, the following message appears:

"Processing ..."

and stays there forever.

My guess is the JS() code, but cannot debug that.

library(shiny)
library(DT)
library(htmltools)

ui <- fluidPage(


  fluidRow(
    column(9, DT::dataTableOutput('withtotal'))
  )

)
server <- function(input, output, session) {

  # server-side processing
  mtcars2 = mtcars[, 1:8]
  #sketch <- htmltools::withTags(table(tableHeader(mtcars2), tableFooter(mtcars2)))

  sketch = htmltools::withTags(table(tableFooter(c("",0,0,0,0,0,0,0))))

 opts <- list( footerCallback = JS("function ( row, data, start, end, display ) {",
     "var api = this.api();",
     "var intVal = function ( i ) {",
      "return typeof i === 'string' ?",
       "i.replace(/[\\$,]/g, '')*1 :",
         "typeof i === 'number' ?",
         "i : 0;",
     "};",
     "if (api.column(COLNUMBER).data().length){",
       "var total = api",
       ".column( COLNUMBER )",
       ".data()",
       ".reduce( function (a, b) {",
         "return intVal(a) + intVal(b);",
       "} ) }",
     "else{ total = 0};",
     "if (api.column(COLNUMBER).data().length){",
       "var pageTotal = api",
       ".column( COLNUMBER, { page: 'current'} )",
       ".data()",
       ".reduce( function (a, b) {",
        " return intVal(a) + intVal(b);",
       "} ) }",
    "else{ pageTotal = 0};",
     "$( api.column(COLNUMBER).footer() ).html(",
       "'$'+pageTotal",
     ");",
   "}"))


  output$withtotal = DT::renderDataTable(DT::datatable(mtcars2,container = sketch, options = opts))      


}

options(shiny.error = browser)
# Run the application 
shinyApp(ui = ui, server = server)
like image 713
MPaydar Avatar asked Mar 21 '17 17:03

MPaydar


People also ask

How do I get the total of a column in a DataTable?

HTML Code for DataTables with Column Total We set the serverSide option as true to get the database result by using AJAX call. This code contains table footer markup with empty container tags. These containers will be filled with the sum of the column data after executing footer callback functions.

How do you add data to a data table?

After you create a DataTable and define its structure using columns and constraints, you can add new rows of data to the table. To add a new row, declare a new variable as type DataRow. A new DataRow object is returned when you call the NewRow method.


1 Answers

This is a version without using Shiny. I used the same JavaScript as @gscott above, but this is for a standalone table. I used this in a RMarkdown document. The key to this, which I struggled with, is the container argument, which adds the footer to the table.

library(htmlwidgets)
library(DT)
library(htmltools)

sketch <- htmltools::withTags(table(
  tableHeader(colnames(mtcars)), 
  tableFooter(c(0,0,0,0,0,0,0,0,0,0,0,0))
))

jsCode <- "function(row, data, start, end, display) {
  var api = this.api(), data;
  total = api.column(7, {page: 'current'}).data().reduce( function(a, b) { return a + 
b}, 0);
  total2 = api.column(6, {page: 'current'}).data().reduce( function(a, b) { return a 
+ b}, 0);
  total3 = api.column(2, {page: 'current'}).data().reduce( function(a, b) { return a 
+ b}, 0);
  $( api.column(7).footer() ).html('Total: ' + total.toFixed(2));
  $( api.column(6).footer() ).html('Total: ' + total2.toFixed(2));
  $( api.column(2).footer() ).html('Total: ' + total3.toFixed(2))
  }"

DT::datatable(mtcars, container = sketch, options=list(scrollY=300, scrollX=TRUE, scroller=TRUE, footerCallback = JS(jsCode)))
like image 162
Dan Carpenter Avatar answered Oct 17 '22 10:10

Dan Carpenter