Search code examples
rshinydt

Datatables Buttons to show/hide RowGroups


I have a table that uses RowGroups and Buttons/Colvis.

Much like the buttons for Colvis, I'd like to have buttons to show only specific RowGroups. Currently I have a long table with quite a few RowGroups. Often users will want to see only specific column groups and I'd like to prevent them from having to scroll down all the way constantly.

From the examples below, imagine there being a button "Row group" visibility, exactly like the Column visiblity in Colvis.

Example, imagine there also being a "Row visibility" button.


Solution

  • What I described can be accomplished using the SearchPanes extension. You can select your searchPanes targets on the same columns as your rowGroup targets.

    You can also set your searchPanes to be a button.

    Example from my code, with use in renderDT:

    output$tbl <- renderDT({
            datatable(data,
                      rownames = FALSE,
                      caption = 'Some caption',
                      extensions = c('ColReorder', 'RowGroup', "Buttons",
                                     "Select", "SearchPanes"),
                      options = list(#DOM options
                                     dom = "Bfrtip",
                                     #Rowgroup options
                                     rowGroup = list(dataSrc = c(0,2)),
                                     #Buttons options
                                     stateSave = TRUE,
                                     buttons = c('colvis', 'csv', 'excel', 'searchPanes'),
                                     #ColReorder options
                                     colReorder = TRUE,
                                     #SearchPanes options
                                     columnDefs = list(
                                       list(searchPanes = list(show = FALSE), targets = 1:7)),
                                     selection = 'none',
                                     #Overige options
                                     paging = FALSE)
          )}, server = FALSE)
    

    Also credit to Matt Herman's blog post: https://mattherman.info/blog/dt_searchpanes/