I'm trying to adapt this method for capturing DT's column search term responses and re-applying them when table refreshes due to an update of its reactive data object. I can't figure out the right formulation to achieve this. Reproducible example:
require(shiny)
require(dplyr)
require(stringr)
require(DT)
d = tibble(sentence = sentences, chars = nchar(sentence), grp = sample(LETTERS, length(sentences), replace = TRUE))
default_search = ''
ui <- fluidPage(
selectInput("grp", 'group', choices = LETTERS, selected = NULL),
DTOutput("data_tbl")
)
server <- function(input, output, session) {
r = reactiveValues(group = NULL, lines = NULL, search_columns = NULL)
proxy <- dataTableProxy('data_tbl')
observeEvent(input$grp, {
r$data = d %>% filter(grp == input$grp)
})
output$data_tbl <- renderDT(filter = "top", {
r$data %>% select(-grp)
}, options = list(stateSave = TRUE))
# catch column search terms
observeEvent(input$data_tbl_search_columns, {
if(is.null(input$data_tbl_search_columns)) return()
if(input$data_tbl_search_columns[1] != '') {
proxy %>% updateSearch(keywords = list(global = default_search, columns = c('', isolate(input$data_tbl_search_columns))))
}
})
}
shinyApp(ui, server)
The intended outcome would be:
- User adds a search filter in "sentence" field e.g. "the". The table filters to matching records.
- User changes the grp selection to change the data. The table refreshes, but (i) the search term is preserved in the filter window, and (ii) the table is filtered accordingly.
I'm getting some persistence but the results are wrong. And the filter term is omitted from the search window. Any assistance appreciated.