Updating filters in shiny app

前端 未结 2 1766
面向向阳花
面向向阳花 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:13

    You posted a comment on my post saying that you were having the same problem as me. It looks a little bit different, but I found a solution to my problem so I've posted the code below in case it helps you at all.

    l <- NULL
    l$name <- c('b','e','d','b','b','d','e','e','b','b')
    l$age <- c(20,20,21,21,20,22,22,30,21,32)
    l$gender <- c('Female', 'Female', 'Male', 'Female', 'Male','Male', 
    'Female','Male',"Female","Male")
    l <- as.data.frame(l)
    l$name <- as.character(l$name)
    l$age <- as.numeric(l$age)
    l$gender <- as.character(l$gender)
    
    
    library(shiny)
    server <- shinyServer(function(input,output){
    
    assign('All Names',unique(sort(l$name)))
    assign("All Ages", unique(sort(l$age)))
    assign('All Genders', unique(sort(l$gender)))
    data1 <- reactive(l[which(l$name %in% if(exists(input$name))
    {get(input$name)}else{input$name}),])
    
    output$table1 <- renderTable(data1())
    output$text1 <- renderPrint(input$name)
    data2 <- reactive(data1()[which(data1()$age %in% if(exists(input$age))
    {get(input$age)}else{input$age}),])
    output$table2 <- renderTable(data2())
    data3 <- reactive(data2()[which(data2()$gender %in% if(exists(input$gender))
    {get(input$gender)}else{input$gender}),])
    
    output$table3 <- renderTable(data3())
    
    
    output$Box1 =  renderUI(
    if((is.null(input$age)) & (is.null(input$gender))){
      selectInput("name", "Choose Name", choices=c("All Names",unique(sort(l$name))), selected = input$name)
    } else{selectInput("name", "Choose Name", choices=c("All Names",unique(l[l$gender %in% (if(exists(input$gender)){get(input$gender)}else{input$gender}) & l$age %in% (if(exists(input$age)){get(input$age)}else{input$age}) , "name"])), selected = input$name)
    }
    )
    
    
    
    output$Box2 =  renderUI(
    if((is.null(input$name)) & (is.null(input$gender))){
      selectInput("age", "Choose Age", choices=c("All Ages", unique(sort(l$age))), selected = input$age)
    }else{selectInput("age", "Choose Age", choices=c("All Ages",unique(l[l$gender %in% (if(exists(input$gender)){get(input$gender)}else{input$gender}) & l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) , "age"])), selected = input$age)}
    )
    
    output$Box3 =  renderUI(
      if((is.null(input$name)) & (is.null(input$age))){
        selectInput("gender", "Choose Gender", choices=c("All Genders", unique(sort(l$gender))), selected = input$gender)
      }else{
    
        selectInput("gender", "Choose Gender", choices=c("All Genders", unique(l[l$name %in% (if(exists(input$name)){get(input$name)}else{input$name}) & l$age %in% (if(exists(input$age)){get(input$age)}else{input$age}), "gender"])), selected = input$gender, multiple = TRUE)
      }
    )
    
    
    
    })
    
    ui <-shinyUI(fluidPage(
    uiOutput("Box1"),
    uiOutput("Box2"),
    uiOutput("Box3"),
    tableOutput("table3")
    ))
    
    shinyApp(ui,server)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题