'Update R shiny datatable options without re-rendering

In an R Shiny app, I would like to provide users with the ability to update the initialization options for a datatable. For example, a user may want to search the datatable contents via string literal in some cases, regex in others.

While it's fairly simple to implement this feature, there does not seem to be a way to update the regex option for datatable without resetting the column filters and search entries. I am wondering if it's possible to ensure the options update does not interfere with the search box and the filters. Here's an example case:

library(shiny)
library(DT)

ui <- fluidPage(

  sidebarLayout(
    
    sidebarPanel(
      checkboxInput("useRegex", "Use Regex?")
    ),

    mainPanel(
      dataTableOutput("DT")
    )

  )
)

server <- function(input, output, session) {

  output$DT <- renderDataTable({

    datatable(

      data = iris,

      options = list(
        search = list(regex = input$useRegex, caseInsensitive = TRUE)
      ),

      selection = "single",
      filter = "top"

    )

  })
}

shinyApp(ui = ui, server = server)

I realize that when the regex option is toggled, it's possible to save the state of the datatable, re-initialize, then recreate the state. This works, but it's not scalable. I would imagine the ideal solution might update the options via dataTableProxy, similar to how visNetwork allows updating options through visNetworkProxy.



Sources

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

Source: Stack Overflow

Solution Source