R Shiny:如何从 DT 数据表中的搜索框中获取用户输入?
R Shiny: How to get the user input from the search box in a DT datatable?
我使用 R
中的 shiny
和 shinydashboard
包开发了一个仪表板。仪表板有一个使用 DT
包生成的 datatable
。在 table 的顶部有一个搜索框,允许用户通过输入内容来过滤 table 的行。是否可以获取此输入并将其用于仪表板其他位置的反应式表达式?
这是一个玩具示例:
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
。
我使用 R
中的 shiny
和 shinydashboard
包开发了一个仪表板。仪表板有一个使用 DT
包生成的 datatable
。在 table 的顶部有一个搜索框,允许用户通过输入内容来过滤 table 的行。是否可以获取此输入并将其用于仪表板其他位置的反应式表达式?
这是一个玩具示例:
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
。