three interdependent selectInput in R/Shiny application

后端 未结 1 613
你的背包
你的背包 2021-01-28 04:06

I\'m working on a R/Shiny application and one of its features should be to export data based on some filters. However the filters depends on each other. Consider a list of compa

1条回答
  •  醉梦人生
    2021-01-28 04:50

    I've been looking for a solution for a similar problem and came across this.

    Thanks for the almost-working example! I just switched to selectizeInput and it seems to work for me. Does this meet your need if you are still looking into this?

    One problem, though, is that there's no way of coming back and re-filter because the choices would have been gone. I added a reset filter button to get around that.

    library(shiny)
    library(dplyr)
    
    ## Create dataframe
    group <- rep(toupper(letters[1:3]),each=3)
    department <- c("a","b","c","a","b","d","b","c","d")
    country <- c("IT","FR","DE","IT","DE","HU","HU","FR","FR")
    df <- data.frame(group, department, country)
    
    
    ## Simple user interface with 3 selectInput
    ui <- fluidPage(
      selectizeInput('group', 'Group:', df$group, selected=df$group, multiple=TRUE),
      selectizeInput('dept', 'Department:', df$department, selected=df$department, multiple=TRUE),
      selectizeInput('country', 'Country:', df$country, selected=df$country, multiple=TRUE),
      actionButton("reset_filters", "Reset filters"),
      tableOutput("table1")
    )
    
    
    filter_names <- c("input$group", "input$dept", "input$country")
    filters <- c("group %in% input$group", "department %in% input$dept","country %in% input$country")
    checknull <- NULL
    
    
    server=function(input,output,session) {
    
      ## reactive block to update the choices in the select input fields
      choices <- reactive({
        for (i in seq_along(filter_names)) {
          checknull[i] <- eval(parse(text=paste0("!is.null(", filter_names[i], ")",sep="")))
        }
    
        req(filters[checknull])
        tmp <- eval(parse(text=paste0("filter(df, ", paste0(filters[checknull], collapse = " & "), ")")))
        return(tmp)
      })
    
      ## updateSelectInput
      observe({
        updateSelectizeInput(session,'group', choices=sort(unique(choices()$group)), selected=sort(unique(choices()$group)))
        updateSelectizeInput(session,'dept', choices=sort(unique(choices()$department)), selected=sort(unique(choices()$department)))
        updateSelectizeInput(session,'country', choices=sort(unique(choices()$country)), selected=sort(unique(choices()$country)))
      })
    
      ## reset filters
      observeEvent(input$reset_filters, {
        updateSelectizeInput(session,'group', choices=df$group, selected=df$group)
        updateSelectizeInput(session,'dept', choices=df$department, selected=df$department)
        updateSelectizeInput(session,'country', choices=df$country, selected=df$country)
      })
    
      output$table1 <- renderTable({choices()})
    }
    
    shinyApp(ui,server)
    

    0 讨论(0)
提交回复
热议问题