数据表中的列摘要(来自包DT)

时间:2016-08-24 16:07:09

标签: javascript r datatable dt

是否有可能将摘要添加到R版本的datatable(由DT包提供),就像在这个例子中一样?

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

1 个答案:

答案 0 :(得分:0)

首先,您必须在数据表中添加页脚(使用容器参数),然后在 footerCallback 函数中插入您链接的javascript代码(在< em> options 数据表的参数。)

以下是USArrests数据集的示例:

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