响应式选择器输入,默认配置访客首次访问

Reactive Picker Input, Default Configuration Visitor's First Visit

我正在构建一个闪亮的应用程序,其中包含一个反应式选择器输入。我的反应式选择器输入在访问者 select 一段时间后运行良好(即,它在菜单中显示 Select Country 这一时期的相关国家)。然而,当访问者首次访问应用程序时,Select Country 会提出所有可能的选项,尽管它应该只显示与默认时间段相关的选项(即 selected = period1)。

换句话说,当访客首次访问应用程序时,我如何确保响应式选择器输入也更新?

谢谢!

下面是一个可重现的例子。如果您 运行 此代码然后单击 Select Country 您将看到所有三个国家都可用。现在,如果您 运行 代码,然后单击 Period、select Period 2: X to Y,然后单击 select Period 1: X to Z,然后单击 Select Country 您将看到只有 UKUSA 可用,这是预期的行为!

choice_name <- c('UK','USA','BE','BE')
choice_id <- c(1, 2, 3,3)
period <- c('period1', 'period1', 'period3', 'period3')
data <- data.frame(choice_name, choice_id, period)

choices_picker <- unique(data$choice_id)
names(choices_picker) <- unique(data$choice_name)


ui <- bootstrapPage(                
  absolutePanel(left = 10, bottom = 10, draggable = TRUE, 
  selectInput(inputId = "input_period", label = "Period",
    choices = c("Period 1: X to Z" = "period1", "Period 2: X to Y" = "period2", "Period 3: X to X" = "period3"),
    selected = "period1"),
  pickerInput(inputId = "picker_cty",
    label = "Select Country",
    choices = choices_picker,
    multiple = TRUE),
))

server <- function(input, output, session) {
  
  # Reactive pickerInput ---------------------------------
  observeEvent(input$input_period, {
    
    data1 <- data[data$period == input$input_period,]
    datau <- unique(data$choice_id)
    data1u <- unique(data1$choice_id)
    
    disabled_choices <- ifelse(datau %in% data1u, 0,1)
    
    # Generate reactive picker input
    updatePickerInput(session = session, 
                      inputId = "picker_cty",
                      choices = choices_picker,
                      choicesOpt = list(
                        disabled = disabled_choices,
                        style = ifelse(disabled_choices,
                                       yes = "color: rgba(119, 119, 119, 0.5);",
                                       no = "")
                      ))
  }, ignoreInit = TRUE)
  
}

shinyApp(ui, server)

你需要ignoreNULL=FALSE

 observeEvent(input$input_period, {
    ...
  }, ignoreNULL=FALSE)