【问题标题】:R Shiny: How to get the user input from the search box in a DT datatable?R Shiny:如何从 DT 数据表的搜索框中获取用户输入?
【发布时间】:2021-04-17 11:25:49
【问题描述】:

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

【问题讨论】:

    标签: r shiny shinydashboard dt


    【解决方案1】:

    datatable 函数中使用此回调:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2018-11-01
      • 2019-04-20
      • 2021-05-21
      • 2022-01-20
      • 2018-12-19
      • 2018-07-02
      • 2020-08-12
      • 1970-01-01
      相关资源
      最近更新 更多