簡體   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