'Datatables Select/Deselect All on Page in R Shiny

I've been trying to find a solution to Select/Deselect All on the current page of DataTables, much akin to how the same button works in Gmail. While I can have the Select/Deselect All work on a given page, the logic falls apart when I move to a new page; the Deselect All button is still present on a page where nothing has been selected. I've looked into the 'drawCallback' feature on Datatables, and while it looks promising, I haven't been able to figure out how to integrate this feature into the Shiny app. This is because I use a boolean defined in R called 'selectAll', and I'm not aware of how to successfully send the information between R and JS within 'drawCallback'. Please see the reproducible code down below. Thank you!

library(shiny)
library(DT)

ui <- fluidPage(
  actionButton(
    inputId = "selectAllonPageButton",
    label = "Select All on Page"
  ),
  DTOutput("x3")
)

server <- function(input, output, session) {
  
  output$x3 <- renderDT({
    datatable(iris,
              rownames = FALSE, escape= FALSE,
              options = list(
                drawCallback = JS(
                  '
                    function(settings) {
                          //Not sure what to place here...                      
                    }
                    '
                )
              )
    )
  })
  
  DTProxy <- DT::dataTableProxy('x3')
  
  #Variable that defines whether the button should select/deselect all.
  selectAll <- TRUE
  
  #Code to send selectAll to JS.
  observe({
    session$sendCustomMessage(type='myCallbackHandler', selectAll) 
  })
  
  observeEvent(input$selectAllonPageButton, {
    if (isTRUE(selectAll)){
      selectRows(DTProxy, input$x3_rows_current)
      selectAll <<- FALSE
      updateActionButton(session, "selectAllonPageButton",
                         label = "Unselect All on Page")
    }
    else{
      selectRows(DTProxy, NULL)
      selectAll <<- TRUE
      updateActionButton(session, "selectAllonPageButton",
                         label = "Select All on Page")
    }
  })
}

shinyApp(ui, server)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source