Jaskeil
Jaskeil

Reputation: 1232

How can I create a global search box across multiple DataTables via dataTableProxy in a shiny app?

I have this reproducible code below using the Iris dataset. I am wondering how I can create an additional table below the current table or in a better scenario how I can create a tabset tab to include an additional table.

Code and Screenshot of Output below:

library(shiny)
library(DT)    

shinyApp(
  ui = fluidPage(
    textInput('search2', "Search 2"),
    DTOutput('dt')
  ),
  server = function(input, output, session) {
    
    DTproxy <- dataTableProxy("dt")
    output$dt = renderDT(iris)
    
    observeEvent(input$search2, {
      updateSearch(DTproxy, keywords = list(global = input$search2, columns = NULL))
    })
    
  })

enter image description here

Upvotes: 3

Views: 470

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33397

I guess this is a follow-up on this?

Here is a simple example with 3 tables and 3 tabs all depending on a global search field. We could reduce the code via lapply or modules but I think this is more understandable at first:

library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("Tabsets"),
  sidebarLayout(
    sidebarPanel(
      textInput('search', "Search"),
    ),
    mainPanel(
      tabsetPanel(id = "tabsetPanelID",
                  type = "tabs",
                  tabPanel("Tab1", DTOutput('DT1')),
                  tabPanel("Tab2", DTOutput('DT2')),
                  tabPanel("Tab3", DTOutput('DT3'))
      )
    )
  )
)

server <- function(input, output, session) {
  output$DT1 = renderDT(iris)
  DTProxy1 <- dataTableProxy("DT1")
  
  output$DT2 = renderDT(iris)
  DTProxy2 <- dataTableProxy("DT2")
  
  output$DT3 = renderDT(iris)
  DTProxy3 <- dataTableProxy("DT3")
  
  observeEvent(c(input$search, input$tabsetPanelID), {
    updateSearch(DTProxy1, keywords = list(global = input$search, columns = NULL))
    updateSearch(DTProxy2, keywords = list(global = input$search, columns = NULL))
    updateSearch(DTProxy3, keywords = list(global = input$search, columns = NULL))
  })
}

shinyApp(ui, server)

Upvotes: 3

Related Questions