0

I currently have three inputs that are all dependent on each other. Each successive input is updated based on the value of the previous selection. The code for this is as follows:

library(dplyr)
library(shiny)

    metric <- c('Opioid prescribing rate', 'Opioid prescribing rate','Opioid prescribing rate','Opioid prescribing rate','Opioid prescribing rate', 'Opioid prescribing rate',
              'Opioid prescribing rate','Opioid prescribing rate','Opioid prescribing rate','Opioid prescribing rate','Opioid prescribing rate','Rate of new cancers',
              'Rate of cancer deaths', 'Arthritis prevalence', 'Percent of population over 65', 'Percentage of civilian non-institutionalized population ages 18-64 with a disability',
              'Median household income')
  year <- c(2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,9999,9999,9999,9999,9999,9999)
  year_range <- c('','','','','','','','','','','','2011-2015','2011-2015','2015','2010','2010','2013-2015')
  category <- c('Overdose rates and prescriptions','Overdose rates and prescriptions','Overdose rates and prescriptions','Overdose rates and prescriptions',
                'Overdose rates and prescriptions','Overdose rates and prescriptions','Overdose rates and prescriptions','Overdose rates and prescriptions',
                'Overdose rates and prescriptions','Overdose rates and prescriptions','Overdose rates and prescriptions','Miscellaneous health metrics and diseases',
                'Miscellaneous health metrics and diseases','Miscellaneous health metrics and diseases','Socioeconomic','Socioeconomic','Socioeconomic')

  healthmapyeardata <- as.data.frame(cbind(metric, year, year_range, category))

  ui <- fluidPage(selectInput("maphealthcategory", "Category:", c("Overdose rates and prescriptions",
                                                                   "Miscellaneous health metrics and diseases",
                                                                   "Socioeconomic"), selected = "Overdose rates and prescriptions"),
                  selectInput("maphealthmetric", "Metric:", c("")),

                  conditionalPanel("output.healthmetrics", selectInput("maphealthyear", "Year:", choices = c("All years" = ""))),

                  conditionalPanel("output.healthmetrics === false", htmlOutput("healthmapyearrange"))
                  )

  server <- function(input, output, session) {
    observe({
      metrics <- filter(healthmapyeardata, category %in% input$maphealthcategory) %>%
        `$`('metric') %>%
        unique() %>%
        sort()

      updateSelectInput(session,
                        "maphealthmetric",
                        choices = metrics,
                        selected = metrics[1])

      years <- if (is.null(input$maphealthmetric))
        character(0)
      else {
        filter(healthmapyeardata, metric %in% input$maphealthmetric) %>%
          `$`('year') %>%
          unique() %>%
          sort()
      }

      updateSelectInput(session,
                        "maphealthyear",
                        choices = years,
                        selected = years[1])
    })

    output$healthmetrics <- reactive({
      metrics <- as.list(healthmapyeardata %>% filter(year != 9999) %>% select(metric) %>% unique())
      input$maphealthmetric %in% metrics
    })

    # COPY - if chosen metric only has aggregated data, then this text will render
    period <- reactive({as.character(healthmapyeardata %>% filter(metric == input$maphealthmetric) %>% select(year_range))})

    output$healthmapyearrange <- renderUI({
      HTML(paste0("Time period: ", period()))
    })
    outputOptions(output, "healthmetrics", suspendWhenHidden = FALSE)
  }

  shinyApp(ui = ui, server = server)

According to this code, if I select "Miscellaneous health metrics and diseases" as a category, my input$maphealthmetric should be populated with c('Percent of population over 65', 'Percentage of civilian non-institutionalized population ages 18-64 with a disability',Median household income') and should have "Median household income" selected. This works as it should. However, every time I try to select a different input$maphealthmetric, it automatically resets to "Median household income". I have also tried to remove the line selected = metrics[1] but that doesn't seem to help. How can I fix this?

LocoGris
  • 4,432
  • 3
  • 15
  • 30
ANam
  • 337
  • 2
  • 15
  • What should the selected value be if `inpiut$maphealthcategory == 'Overdose rates and prescriptions'`? – divibisan Mar 04 '19 at 15:50
  • @divibisan It would be "Opioid prescribing rate" and `input$maphealthyear` would populate with all the "non-9999" values. – ANam Mar 04 '19 at 15:53

0 Answers0