简体   繁体   English

ObserveEvent 未触发 R Shiny

[英]ObserveEvent not Triggering in R Shiny

I have a shiny app where I am using observeEvent to watch for a particular action (a radiobutton selection from one of three choices: "year", "quarter", and "month").我有一个 shiny 应用程序,我在其中使用observeEvent来监视特定操作(从三个选项之一中选择一个单选按钮:“年”、“季度”和“月”)。 Depending on this selection, a selectInput dropdown is populated in the UI with a year vector (eg 2012, 2013, etc.), a quarter vector (2012-4, 2013-1, etc.) or a month vector (2012-11, 2012-12, 2013-1, etc).根据此选择,UI 中的 selectInput 下拉列表会填充年份向量(例如 2012、2013 等)、四分之一向量(2012-4、2013-1 等)或月份向量(2012-11 , 2012-12, 2013-1, 等等)。

This code works when the app starts up.此代码在应用程序启动时起作用。 For whichever radiobutton value is selected all the code works properly.无论选择哪个单选按钮值,所有代码都可以正常工作。

However, if I change the radiobutton selection (the observeEvent trigger), the correct vector for the new selection isn't generated and populated in the UI selectInput(for example, if the default radiobutton was "year" and I select "quarter", I find that the proper vector for quarter isn't generated and populated in the UI SelectInput).但是,如果我更改单选按钮选择(observeEvent 触发器),则不会在 UI selectInput 中生成和填充新选择的正确向量(例如,如果默认单选按钮是“年”,而我 select “季度”,我发现未在 UI SelectInput 中生成和填充季度的正确向量)。 This causes a crash and downstream errors.这会导致崩溃和下游错误。

Can anyone advise why this doesn't work?谁能告诉我为什么这不起作用?

Code is below.代码如下。

ui <- fluidPage(
    selectInput(inputId = "date_range_reg_univ_cohorts_from",
                label = "Select Registration Range From",
                "")

   )

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

  observeEvent(
    
    input$ui_button_period_2D_conv,{
    
    if(input$ui_button_period_2D_conv == 'year'){
      
      conv_dates <- date_slicing$cohort_year_from_vec
      
      conv_dates <- paste0(year(conv_dates))

      updateSelectInput(
        session,
        "date_range_reg_univ_cohorts_from",
        choices = conv_dates)
      
    }else if(input$ui_button_period_2D_conv == 'quarter'){
      
      conv_dates <- date_slicing$cohort_quarter_from_vec

      conv_dates <- paste0(year(conv_dates)," - ",quarter(conv_dates))
      
      updateSelectInput(
        session,
        "date_range_reg_univ_cohorts_from",
        choices = conv_dates)
      
      
    }else if(input$ui_button_period_2D_conv == 'month'){
      
      conv_dates <- date_slicing$cohort_month_from_vec
   
      conv_dates <- paste0(year(conv_dates)," - ",month(conv_dates))
      
      updateSelectInput(
        session,
        "date_range_reg_univ_cohorts_from",
        choices = conv_dates)
      
      
    }
    
    }
    
  )

}  

Perhaps you should use eventReative() first and then use observeEvent() as shown below.也许您应该先使用eventReative() ,然后再使用observeEvent() ,如下所示。

year <- c(2010:2015)
month <- c(1:12)
quarter <- c(1:4)

ui <- fluidPage(

  radioButtons("ui_button_period_2D_conv", "Choose", choices = c("Year" = "year", "Month" = "month", "Quarter" =  "quarter") ),
  selectInput(inputId = "date_range_reg_univ_cohorts_from",
              label = "Select Registration Range From",
              "")

)

server <- function(input, output, session) {
  
  conv_dates <- eventReactive(input$ui_button_period_2D_conv, {
    if(input$ui_button_period_2D_conv == 'year'){
      
      conv_dates <- paste0(year)
      
    }else if(input$ui_button_period_2D_conv == 'quarter'){
      
      conv_dates <- paste0(rep(year, each=max(quarter)),"-",quarter)
      
    }else if(input$ui_button_period_2D_conv == 'month'){
      
      conv_dates <- paste0(rep(year, each=max(month)),"-",month)
      
    }
    conv_dates
  })

  observeEvent(input$ui_button_period_2D_conv, {
    req(conv_dates())
    updateSelectInput(session, "date_range_reg_univ_cohorts_from", choices = conv_dates())
  })

}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM