【发布时间】:2018-10-09 21:48:04
【问题描述】:
我一直在使用以下代码来允许多个选择输入相互反应。因此,当其中一个被更改时,其他框中的值也会更新:
l <- NULL
l$name <- c('b','e','d','b','b','d','e')
l$age <- c(20,20,21,21,20,22,22)
l <- as.data.frame(l)
l$name <- as.character(l$name)
l$age <- as.numeric(l$age)
library(shiny)
server <- shinyServer(function(input,output, session){
data1 <- reactive({
if(input$Box1 == "All"){
l
}else{
l[which(l$name == input$Box1),]
}
})
data2 <- reactive({
if (input$Box2 == "All"){
l
}else{
l[which(l$age == input$Box2),]
}
})
observe({
if(input$Box1 != "All"){
updateSelectInput(session,"Box2","Choose an age", choices = c("All",unique(data1()$age)))
}
else if(input$Box2 != 'All'){
updateSelectInput(session,"Box1","Choose a name", choices = c('All',unique(data2()$name)))
}
else if (input$Box1 == "All" & input$Box2 == "All"){
updateSelectInput(session,"Box2","Choose an age", choices = c('All',unique(l$age)))
updateSelectInput(session,"Box1","Choose a name", choices = c('All',unique(l$name)))
}
})
data3 <- reactive({
if(input$Box2 == "All"){
data1()
}else if (input$Box1 == "All"){
data2()
}else if (input$Box2 == "All" & input$Box1 == "All"){
l
}
else{
l[which(l$age== input$Box2 & l$name == input$Box1),]
}
})
output$table1 <- renderTable({
data3()
})
})
ui <-shinyUI(fluidPage(
selectInput("Box1","Choose a name", choices = c("All",unique(l$name))),
selectInput("Box2","Choose an age", choices = c("All",unique(l$age))),
tableOutput("table1")
))
shinyApp(ui,server)
这适用于 2 个选择输入框,但我不知道如何添加更多。
我总共有 4 个选择输入需要相互反应(以及更新反应数据框)。
我是 R 和 Shiny 的新手。
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
感谢不会再发生
-
您能解释一下为什么要对表格进行子集化吗?无论您只是想显示过滤后的数据还是在后续计算中实际使用它,
DT包都具有该功能 - 无需自己做 -
所以我只需要过滤表格中显示的内容。 I have a number of call out boxes and a table that are dynamically updated when one of the options in the select inputs is selected.
-
过滤完成后不进行任何计算。值只是显示。
标签: r shiny subset dropdown reactive