Pass search value from one datatable in R Shiny to multiple other datatables?

547 views Asked by At

I have an Shiny app with a bunch of datatables using the DT package in it. There is one main datatable called records.

What I would like to happen:

When someone enters a value into the DT search bar of the records datatable, i would like automatically pass that search parameter value to all the other datatables search parameter. This would update anytime someone edits the search parameter in the records table.

I have looked into creating a global search box, but i am saving that as a last resort. The built-in datatable/DT package searching is much more efficient and natural to use, i would like to leverage it.

1

There are 1 answers

1
Remko Duursma On BEST ANSWER

Here is an example of two datatables with linked search.

The current search box value is available as input$dt1_state$search$search if your datatable ID is dt1. You can then use DT::updateSearch to set the search box for the next datatable.

library(shiny)
library(DT)
library(dplyr)

table_data <- mtcars %>%
  select(cyl, disp)
table_data$carname <- rownames(table_data)

ui <- fluidPage(
  
  fluidRow(
    column(6,
           DT::dataTableOutput("dt1")         
    ),
    column(6,
           DT::dataTableOutput("dt2")         
    )
  )
    
)

server <- function(input, output, session) {
  
  
  
  dt1_search <- reactive({
    input$dt1_state$search$search
  })
  
  observeEvent(dt1_search(), {
    prox <- DT::dataTableProxy("dt2")
    updateSearch(prox, keywords = list(global = dt1_search()))
  })
  
  output$dt1 <- DT::renderDataTable({
    datatable(table_data)
  })
  
  output$dt2 <- DT::renderDataTable({
    datatable(table_data)
  })
  
}

shinyApp(ui, server)