title: How To Add totals to a DT::datatable?
body: <p>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: </p>
  
  <blockquote>
    <p>"Processing ..." </p>
  </blockquote>
  
  <p>and stays there forever.</p>
  
  <p>My guess is the JS() code, but cannot debug that. </p>
  
  <pre><code>library(shiny)
  library(DT)
  library(htmltools)
  
  ui &lt;- fluidPage(
  
  
    fluidRow(
      column(9, DT::dataTableOutput('withtotal'))
    )
  
  )
  server &lt;- function(input, output, session) {
  
    # server-side processing
    mtcars2 = mtcars[, 1:8]
    #sketch &lt;- htmltools::withTags(table(tableHeader(mtcars2), tableFooter(mtcars2)))
  
    sketch = htmltools::withTags(table(tableFooter(c("",0,0,0,0,0,0,0))))
  
   opts &lt;- 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)
  </code></pre>