如何防止updateSelectInput默认为第一个值?

时间:2019-03-04 15:45:50

标签: r shiny selectinput

我目前有三个相互依赖的输入。每个后续输入都根据先前选择的值进行更新。的代码如下:

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)

根据此代码,如果我选择“其他健康指标和疾病”作为类别,则我的input$maphealthmetric应该填充c('Percent of population over 65', 'Percentage of civilian non-institutionalized population ages 18-64 with a disability',Median household income'),并且应该选择“家庭收入中位数”。这可以正常工作。但是,每次我尝试选择其他input$maphealthmetric时,它都会自动重置为“家庭收入中位数”。我也尝试过删除行selected = metrics[1],但这似乎无济于事。我怎样才能解决这个问题?

0 个答案:

没有答案