Updating filters in shiny app

前端 未结 2 1775
面向向阳花
面向向阳花 2020-12-12 02:40

I have an app with updating filters but seems not to work correctly I can\'t fix it. I want all filters updating when I change a depending filter I think the problem is abou

2条回答
  •  盖世英雄少女心
    2020-12-12 03:28

    I answered to a similar question that you commented (saying you've got the same problem) with this solution :

    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)
    

提交回复
热议问题