R Shiny中未触发观察事件

  • 本文关键字:观察 事件 Shiny r shiny
  • 更新时间 :
  • 英文 :


我有一个闪亮的应用程序,我正在使用observeEvent监视特定的动作(从三个选项中选择一个单选按钮:"年"、"季度"one_answers"月"(。根据此选择,用户界面中的selectInput下拉列表将填充年份矢量(例如2012、2013等(、季度矢量(2012-4、2013-1等(或月份矢量(2012-11、2012-12、2013-1等等(。

此代码在应用程序启动时有效。无论选择哪个单选按钮值,所有代码都能正常工作。

但是,如果我更改单选按钮选择(observeEvent触发器(,则不会在UI selectInput中生成和填充新选择的正确矢量(例如,如果默认单选按钮为"年",而我选择"季度",我会发现在UI selectInput中没有生成和填充季度的正确矢量(。这会导致崩溃和下游错误。

有人能告诉我为什么这不起作用吗?

代码如下。

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)


}

}

)
}  

也许您应该先使用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)

最新更新