Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a footer in DT

Tags:

css

r

dt

I'm trying to add a colsum in a footer of a datatable

I did it for 1 column with https://github.com/rstudio/DT/issues/39

sketch <- htmltools::withTags(table(
  tableHeader(cars),
  tableFooter(cars)
))


datatable(cars,container = sketch, rownames = F, 
      options = list(
        footerCallback = JS(
          "function( tfoot, data, start, end, display ) {",
          "var api = this.api();",
          "$( api.column(1).footer() ).html(",
          "api.column(1 ).data().reduce( function ( a, b ) {",
          "return a + b;",
          "} )",  # remove ; here
          ");",
          "}")
      )
)

I'm trying to do it for
- all column
- all num column (or all except char id it's easier)

edit: the solution :)

dt_test <- structure(list(`pathologie principale` = c("Effet toxique des métaux", 
                                                  "Autres résultats anormaux des examens chimiques du sang", "Néphrite tubulo-interstitielle chronique", 
                                                  "Atteintes tubulo-interstitielles et tubulaires dues à des médicaments et des métaux lourds", 
                                                  "Autres maladies     pulmonaires obstructives chroniques", "Autres résultats anormaux de l'examen des urines"
),     Fort = c(12L, 4L, 3L, 2L, 2L, 2L), Moyen = c(2L, 0L, 0L, 0L, 1L, 1L), Faible = c(4L, 0L, 0L, 0L, 4L, 0L)),   
.Names = c("pathologie principale",                  "Fort", "Moyen", "Faible"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L))


sketch <- htmltools::withTags(table(
  tableHeader(dt_test),
  tableFooter(sapply(dt_test, function(x) ifelse( (is.numeric(x)) ,sum(x)     ,"total" ))
)))


datatable(dt_test,
      container = sketch, 
      rownames = F
)
like image 983
s.brunel Avatar asked Mar 07 '17 15:03

s.brunel


2 Answers

There is no need of a footerCallback:

sketch <- htmltools::withTags(table(
    tableHeader(dt_test),
    tableFooter(sapply(dt_test, function(x) if(is.numeric(x)) sum(x)))
))


datatable(dt_test,
          container = sketch, 
          rownames = F
)

enter image description here

like image 76
GGamba Avatar answered Nov 01 '22 01:11

GGamba


Edit: GGamba's answer is simpler and should be used, I still want to keep the right JS code to use with the footerCallback to work on every column separately for future reference.

Insert the call into a for loop from 0 (or any column you want) to end:

opts <- list(
footerCallback = JS(
"function( tfoot, data, start, end, display ) {",
"var api = this.api();",
sprintf("for(var i=1; i<%d; i++) {",ncol(dt_test)),
"  $( api.column(i).footer() ).html(",
"  api.column(i ).data().reduce( function ( a, b ) {",
"    if(isNaN(a)) return ''; return a+b;",
"  } )",  # remove ; here
"  );",
"}}"))

datatable(dt_test, container = sketch, options = opts)

Note that the first column is a string, so the reduce concatenates the string. Also note that you can do so for any number between start and end.

like image 2
Shahar Bental Avatar answered Nov 01 '22 00:11

Shahar Bental