[英]ObserveEvent not Triggering in R Shiny
我有一个 shiny 应用程序,我在其中使用observeEvent
来监视特定操作(从三个选项之一中选择一个单选按钮:“年”、“季度”和“月”)。 根据此选择,UI 中的 selectInput 下拉列表会填充年份向量(例如 2012、2013 等)、四分之一向量(2012-4、2013-1 等)或月份向量(2012-11 , 2012-12, 2013-1, 等等)。
此代码在应用程序启动时起作用。 无论选择哪个单选按钮值,所有代码都可以正常工作。
但是,如果我更改单选按钮选择(observeEvent 触发器),则不会在 UI selectInput 中生成和填充新选择的正确向量(例如,如果默认单选按钮是“年”,而我 select “季度”,我发现未在 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.