Column summary in datatable (from DT package)

Is it possible to add a summary to the R-version of datatable (provided by the DT package), as in this example?

https://datatables.net/examples/advanced_init/footer_callback.html

+6
source share
1 answer

First you need to add a footer to your datatable (using the container parameter), and then insert the javascript code that you linked in the footerCallback function (in the parameter parameter for the data).

Here is an example with USArrests datasets:

sketch <- htmltools::withTags(table( tableHeader(c('State', names(USArrests))), tableFooter(rep('', 5)) )) #here is a copy of the javascript you had link opts <- list( footerCallback = JS("function( row, data, start, end, display ) {", "var api = this.api(), data;", "var intVal = function ( i ) {", "return typeof i === 'string' ?", "i.replace(/[\\$,]/g, '')*1 :typeof i === 'number' ?", "i : 0;};", "total = api", ".column( 3 )", ".data()", ".reduce( function (a, b) {", " return intVal(a) + intVal(b);", " }, 0 );", "$( api.column( 3 ).footer() ).html('('+ total +' total)');", "}")) datatable(USArrests, container = sketch, options = opts) 

enter image description here

0
source

All Articles