R Shiny:如何从DT数据表中的搜索框中获得用户输入



我使用R中的shinyshinydashboard包开发了一个仪表板。仪表板上有一个使用DT软件包生成的datatable。在表的顶部,有一个搜索框,用户可以通过键入输入来过滤表中的行。是否可以获取此输入并将其用于仪表板中其他位置的反应式表达式?

这里有一个玩具示例:

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!" })
}
)

datatable函数中使用此回调:

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

然后,在Shiny服务器中,用户输入了反应变量input$search

最新更新