0

I have developed a dashboard using the shiny and shinydashboard packages in R. The dashboard has a datatable produced with the DT package. At the top of the table, there is a search box that allows users to filter the rows of the table by typing in an input. Is it possible to get this input and use it in a reactive expression elsewhere in the dashboard?

Here is a toy example:

library(shiny)
library(shinydashboard)
library(DT)

shinyApp(
  ui = shinyUI(
    dashboardPage(
      dashboardHeader(title = "Example"),
      dashboardSidebar(sidebarMenu(id = 'tabs', menuItem("Tab1", tabName = 'Tab1'))), 
      dashboardBody(tabItems(tabItem(tabName = "Tab1", 
                                     fluidRow(column(width=6,box(DT::dataTableOutput('myTable'), width=NULL)),
                                              column(width=6,box(textOutput("myText"), width=NULL))))))
      )
  ),
  
  server = function(input, output, session){
    mytbl <- data.frame(Name = c('Matthew', 'Luke', 'John'), Grade=c(45, 20, 80))
    output$myTable <- DT::renderDataTable({DT::datatable(mytbl, rownames=FALSE)})
    output$myText <- renderText({ "The value entered in the seach box should appear here!" })
  }
)
Miguel
  • 416
  • 3
  • 16

1 Answers1

2

Use this callback in the datatable function:

callback = JS(
  "table.on( 'search.dt', function () {",
    "Shiny.setInputValue( 'search', table.search() );",
  "} );"
)

Then in the Shiny server you have the user input in the reactive variable input$search.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225