[英]R Shiny: How to get the user input from the search box in a DT datatable?
I have developed a dashboard using the shiny
and shinydashboard
packages in R
.我使用
R
中的shiny
和shinydashboard
包开发了一个仪表板。 The dashboard has a datatable
produced with the DT
package.仪表板有一个使用
DT
datatable
生成的数据表。 At the top of the table, there is a search box that allows users to filter the rows of the table by typing in an input.在表格的顶部,有一个搜索框,允许用户通过输入来过滤表格的行。 Is it possible to get this input and use it in a reactive expression elsewhere in the dashboard?
是否可以获取此输入并将其用于仪表板其他地方的反应式表达式?
Here is a toy example:这是一个玩具示例:
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!" })
}
)
Use this callback in the datatable
function:在数据表
datatable
中使用此回调:
callback = JS(
"table.on( 'search.dt', function () {",
"Shiny.setInputValue( 'search', table.search() );",
"} );"
)
Then in the Shiny server you have the user input in the reactive variable input$search
.然后在 Shiny 服务器中,您在反应变量
input$search
中有用户输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.