Nate P
Nate P

Reputation: 23

Is there an easier/tidier/better way of creating multiple, relative dynamic user selection drop downs?

I'm building a shiny dashboard and I would like to have the choices available to the user reflect the choices the user has already made. Something like:

First selector: Option A or Option B

Second selector:

 if Option A: Choice A, Choice B, Choice C

 if Option B: Choice D, Choice E, Choice F

etc.

I'm wondering if there's a simpler, more concise way of doing it than how I did it (i.e. nested if else calls).

Here's how I did it (this code doesn't really rely on any data - you should be able to huck this into your console and run it - minus the path stuff).

library(shiny)
library(shinydashboard)
library(tidyverse)

################################################################################
## Global variables

start_year <- 2016
end_year <- 2021
afh <- NULL
alrc <- NULL

################################################################################
## Define UI

ui <- dashboardPage(
  
  ## General formatting
  skin = "black", 
  
  ## Title

  ## Header
  dashboardHeader(),
  
  ## Sidebar and inputs
  dashboardSidebar(
    ## formatting
    tags$style(type = "text/css", ".irs-grid-pol.small {height: 0px;}"),
    
    ## pane title
    selectInput("dataset", "Dataset", 
                c("-", 
                  "Adult Foster Home Resident and Community Characteristics", 
                  "Assisted Living/Residential Care Community Characteristics")
                ), 
    selectInput("category", "Category", "*Please select a dataset*"), 
    selectInput("statistic", "Statistic", "*Please select a category*"), 
    selectInput("stratification", "Stratification", c("None", "Region", "County")), 
    sliderInput(
      inputId = "year",
      label = "Year Range",
      value = c(start_year, end_year),
      min = start_year,
      max = end_year,
      step = 1,
      sep = ""
    )
  ),
  
  ## Body and outputs
  dashboardBody()
)

################################################################################
## Define Server components

server <- function(input, output, session) {
  
  ## Set the dynamic options for the Category dropdown selector
  observeEvent(input$dataset, {
    if (input$dataset == "-") {
      freezeReactiveValue(input, "category")
      updateSelectInput(inputId = "category",
                        choices = c("*Please select a dataset*"))
    } else
      if (input$dataset == "Adult Foster Home Resident and Community Characteristics") {
        freezeReactiveValue(input, "category")
        updateSelectInput(
          inputId = "category",
          choices = c(
            "*Please select a category*",
            "Homes",
            "Owners",
            "Staff",
            "Finances",
            "Residents",
            "COVID-19"
          )
        )
      } else
        if (input$dataset == "Assisted Living/Residential Care Community Characteristics") {
          freezeReactiveValue(input, "category")
          updateSelectInput(
            inputId = "category",
            choices = c(
              "*Please select a category*",
              "Community",
              "Staff",
              "Residents",
              "COVID-19"
            )
          )
        }
  })
  
  ## Set the dynamic options for the Statistic dropdown selector
  observeEvent(input$category, {
    if (input$category == "*Please select a dataset*") {
      freezeReactiveValue(input, "statistic")
      updateSelectInput(inputId = "statistic",
                        choices = c("*Please select a category*"))
    } else
      if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
          input$category == "Homes") {
        freezeReactiveValue(input, "statistic")
        updateSelectInput(
          inputId = "statistic",
          choices = c(
            "*Please select a statistic*",
            "AFH: Number",
            "AFH: Licensed Capacity",
            "AFH: Years of Operation"
          )
        )
      } else
        if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
            input$category == "Owners") {
          freezeReactiveValue(input, "statistic")
          updateSelectInput(
            inputId = "statistic",
            choices = c(
              "*Please select a statistic*",
              "AFH: Future Plans"
            )
          )
        } else
          if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
              input$category == "Staff") {
            freezeReactiveValue(input, "statistic")
            updateSelectInput(
              inputId = "statistic",
              choices = c(
                "*Please select a statistic*",
                "AFH: Tenure", 
                "AFH: Absenteeism", 
                "AFH: Contract Staff"
              )
            )
          } else
            if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
                input$category == "Finances") {
              freezeReactiveValue(input, "statistic")
              updateSelectInput(
                inputId = "statistic",
                choices = c(
                  "*Please select a statistic*",
                  "AFH: Medicaid Acceptance and Use", 
                  "AFH: Medicaid Reimbursement Rates", 
                  "AFH: Private Pay Rates by Region", 
                  "AFH: Additional Private-Pay Services and Charges"
                )
              )
            } else
              if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
                  input$category == "Residents") {
                freezeReactiveValue(input, "statistic")
                updateSelectInput(
                  inputId = "statistic",
                  choices = c(
                    "*Please select a statistic*",
                    "AFH: Demographics", 
                    "AFH: Move-In and Move-Out Locations", 
                    "AFH: Length of Stay", 
                    "AFH: Personal Care Services", 
                    "AFH: Assistance from Two Staff and Nighttime Care", 
                    "AFH: Visits and Assistance from Family Members and Friends", 
                    "AFH: Resident Health Conditions and Falls", 
                    "AFH: Health Service and Medication Use"
                  )
                )
              } else
                if (input$dataset == "Adult Foster Home Resident and Community Characteristics" & input$category == "COVID-19") {
                  freezeReactiveValue(input, "statistic")
                  updateSelectInput(
                    inputId = "statistic",
                    choices = c(
                      "*Please select a statistic*",
                      "AFH: Supports & Challenges"
                    )
                  )
                } else
                  if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "Community") {
                    freezeReactiveValue(input, "statistic")
                    updateSelectInput(
                      inputId = "statistic",
                      choices = c(
                        "*Please select a statistic*",
                        "ALRC: AL/RC/MC Supply Across Oregon", 
                        "ALRC: Ownership, Chain Affiliation, and Multi-License Settings", 
                        "ALRC: Occupancy Rates", 
                        "ALRC: Units and Room Sharing", 
                        "ALRC: Medicaid Acceptance, Medicaid Reimbursement, and Payer Sources", 
                        "ALRC: Private Pay Charges", 
                        "ALRC: Estimated Industry Charges", 
                        "ALRC: Use of Electronic Health Records", 
                        "ALRC: Additional Services"
                      )
                    )
                  } else
                    if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "Staff") {
                      freezeReactiveValue(input, "statistic")
                      updateSelectInput(
                        inputId = "statistic",
                        choices = c(
                          "*Please select a statistic*",
                          "ALRC: Care-Related Staff Employed Full-Time and Part-Time", 
                          "ALRC: Staff to Resident Ratios", 
                          "ALRC: Staffing Levels", 
                          "ALRC: Current Job Openings", 
                          "ALRC: Unplanned Staff Absences and Outside Service Provider Use", 
                          "ALRC: Recent Turnover and Current Staff Tenure"
                        )
                      )
                    } else
                      if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "Residents") {
                        freezeReactiveValue(input, "statistic")
                        updateSelectInput(
                          inputId = "statistic",
                          choices = c(
                            "*Please select a statistic*",
                            "ALRC: Demographics", 
                            "ALRC: Move-In, Move-Out, and Length of Stay", 
                            "ALRC: Length of Stay Among Residents Who Moved", 
                            "ALRC: Hospice Use Among Residents Who Died", 
                            "ALRC: Assistance with Personal Care", 
                            "ALRC: Health Conditions", 
                            "ALRC: Significant Change in Condition", 
                            "ALRC: Falls & Fall-Related Injuries", 
                            "ALRC: Health Service Use", 
                            "ALRC: Medication Use"
                          )
                        )
                      } else
                        if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "COVID-19") {
                          freezeReactiveValue(input, "statistic")
                          updateSelectInput(
                            inputId = "statistic",
                            choices = c(
                              "*Please select a statistic*",
                              "ALRC: Administrator Experiences"
                            )
                          )
                        } 
  })

################################################################################
## Run Application

shinyApp(ui, server)

Upvotes: 0

Views: 58

Answers (1)

YLC
YLC

Reputation: 144

Try using conditionalPanel. I simplified your code as follows. Basically, my recommendation trades the complexity of your code with the simplicity of conditional panels, at the cost of creating more SelectInput.

Note how the category changes depending on your dataset. Also note when dataset B and category T are selected, Statistic and Stratification appear. You should notice there are three different SelectInput("categoryx", ...). Finally, note there are some side effects that need to be worked out. Reach out if you need more help.

library(shiny) library(shinydashboard)

ui <- dashboardPage(

dashboardHeader(),

dashboardSidebar(

selectInput("dataset", "Dataset", c("-", "A", "B")),

conditionalPanel(
  condition = "input.dataset == '-'",
  selectInput("category", "Category", "*Please select a dataset*")
),

conditionalPanel(
  condition = "input.dataset == 'A'",
  selectInput("category1", "Category", choices=c("H", "O", "S"))
),

conditionalPanel(
  condition = "input.dataset == 'B'",
  selectInput("category2", "Category", choices=c("C", "T", "R"))
),

conditionalPanel(
  condition = "input.category2 == 'T'",
  selectInput("statistic", "Statistic", c("*Please select a category*", "X", "Y", "Z")), 
  selectInput("stratification", "Stratification", c("None", "Region", "County")) 
)    

),

dashboardBody() )

server <- function(input, output, session) { }

shinyApp(ui, server)

Upvotes: 1

Related Questions