Search code examples
rdt

How to set multiple option list and extensions in DT::datatable


I tried to apply datatable options and extensions to plot a table. Its work if follow the reference but ColVis doesn't work when I combined / applied multiple extensions. Any idea?

df %>% 
  datatable(., caption="Table 3.4.1 : Partial Matching Teams' Name.", 
            extensions=list('ColReorder','ColVis', list(FixedColumns=list(leftColumns=2))), 
            options=list(autoWidth=TRUE,
                         dom='C<"clear">lfrtip',
                         colVis=list(exclude=c(0, 1),
                         activate='mouseover'),
            colReorder=list(realtime=TRUE),
            scrollX=TRUE,
            scrollCollapse=TRUE))

Solution

  • According to the DT manual options associated with some extensions needs to be placed in a named list. If one specifies options in options attribute, then NULL must be assigned.

    datatable(.,extensions=list("ColReorder" = NULL,
                                "ColVis" = NULL,
                                "FixedColumns"=list(leftColumns=2))
    

    Another error is generated due to insufficient dom attribute. For more info see this link . Each letter in dom is linked with specified element of table output. Uppercase letters associated with extension and lowercase with table elements (R - ColReorder, C - ColVis, T - TableTools, t - table, i - table info etc.). In case example 'R' is missing, and therefore ColReorder couldn't work. Putting all together below correct code with TableTools added:

    iris %>% 
      datatable(
        extensions = list("ColReorder" = NULL,
                          "ColVis" = NULL,
                          "TableTools" = NULL,
                          "FixedColumns" = list(leftColumns=2)), 
        options = list(autoWidth=TRUE,
                       oColReorder = list(realtime=TRUE),
                       oColVis = list(exclude=c(0, 1),   activate='mouseover'),
                       oTableTools = list(
                       sSwfPath = "//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/swf/copy_csv_xls.swf",
                       aButtons = list("copy","print",
                                       list(sExtends = "collection",
                                            sButtonText = "Save",
                                            aButtons = c("csv","xls")))),
                   dom = 'CRTrilftp',
                   scrollX = TRUE,
                   scrollCollapse = TRUE))
    

    Upgrade! As DT has upgraded (v0.1.56) extensions TableTools and ColVis is no longer available. According to the new tutorial above extension are possible through buttons extension. New version of package is more consistent and adding extensions is easier than before:

     DT:::datatable(
        iris,
        escape=F,
        filter = "top",
        rownames= F,
        extensions = list("ColReorder" = NULL,
                          "Buttons" = NULL,
                          "FixedColumns" = list(leftColumns=1)),
        options = list(
                    dom = 'BRrltpi',
                    autoWidth=TRUE,
                    lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
                    ColReorder = TRUE,
                    buttons =
                      list(
                        'copy',
                        'print',
                        list(
                          extend = 'collection',
                          buttons = c('csv', 'excel', 'pdf'),
                          text = 'Download'
                        ),
                        I('colvis')
                      )
                  )
        )