繁体   English   中英

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

[英]R Shiny: How to get the user input from the search box in a DT datatable?

我使用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中有用户输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM