You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny应用中使用observeEvent和eventReactive无法更新输入项问题

问题修复方案

报错根因

  • 响应式对象调用未加括号:seleccionar_opcion_lugar是eventReactive生成的响应式函数,必须添加()才能获取返回的实际值。原代码中choices <- unlist(seleccionar_opcion_lugar)没有加括号,直接将函数对象(closure类型)传入unlist,触发类型转换报错。
  • UI参数位置错误:Lugar下拉框的multiple=TRUE参数写在了selectInput的闭合括号外,属于语法错误,导致参数配置不生效。
  • 冗余逻辑:联动更新不需要同时使用eventReactive和observeEvent,仅用observeEvent即可实现,简化代码的同时降低出错概率。

修正后完整代码

# 加载依赖包
library(shiny)
library(shinythemes)
library(plotly)
library(dplyr)

# 载入数据集
df <- structure(list(año = c("2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021", 
"2021"), opciones = c("L1", "L1", "L1", "L2", "L2", "L2", "L3", 
"L3", "L3", "LA", "LA", "LA", "LB", "LB", "LB", "LC", "LC", "LC", 
"L1", "L1", "L1", "L2", "L2", "L2", "L3", "L3", "L3", "LA", "LA", 
"LA", "LB", "LB", "LB", "LC", "LC", "LC", "L1", "L1", "L1", "L2", 
"L2", "L2", "L3", "L3", "L3", "LA", "LA", "LA", "LB", "LB", "LB", 
"LC", "LC", "LC"), lugar = c("EXT", "INT", "LOC", "NOM", "KHA", 
"POC", "TMO", "MCR", "GNR", "APV", "HOT", "ROR", "GRU", "BOY", 
"POK", "NOT", "LOX", "TAK", "EXT", "INT", "LOC", "NOM", "KHA", 
"POC", "TMO", "MCR", "GNR", "APV", "HOT", "ROR", "GRU", "BOY", 
"POK", "NOT", "LOX", "TAK", "EXT", "INT", "LOC", "NOM", "KHA", 
"POC", "TMO", "MCR", "GNR", "APV", "HOT", "ROR", "GRU", "BOY", 
"POK", "NOT", "LOX", "TAK"), numeros = c(4011L, 18564L, 24325L, 
9798L, 18621L, 11165L, 6071L, 23466L, 22472L, 23990L, 3980L, 
2976L, 24142L, 3140L, 20317L, 11884L, 9427L, 20704L, 609L, 23428L, 
19853L, 10450L, 577L, 14153L, 4757L, 6393L, 18461L, 1988L, 20044L, 
8371L, 21371L, 12312L, 17368L, 16045L, 11492L, 7903L, 21409L, 
12547L, 19926L, 17857L, 6728L, 5584L, 14837L, 10269L, 20168L, 
13092L, 22233L, 20663L, 11975L, 14672L, 19389L, 7329L, 14062L, 
167L), mes = c("Agosto", "Julio", "Agosto", "Febrero", "Julio", 
"Agosto", "Mayo", "Noviembre", "Abril", "Diciembre", "Julio", 
"Febrero", "Mayo", "Octubre", "Septiembre", "Febrero", "Enero", 
"Febrero", "Noviembre", "Julio", "Septiembre", "Marzo", "Diciembre", 
"Octubre", "Enero", "Agosto", "Septiembre", "Abril", "Julio", 
"Enero", "Febrero", "Febrero", "Abril", "Septiembre", "Agosto", 
"Septiembre", "Abril", "Septiembre", "Abril", "Febrero", "Marzo", 
"Febrero", "Abril", "Noviembre", "Noviembre", "Septiembre", "Abril", 
"Enero", "Marzo", "Julio", "Mayo", "Febrero", "Febrero", "Agosto"
)), class = "data.frame", row.names = c(NA, -54L))

# 全局选项配置
años <- c("2019", "2020", "2021")
meses <- c("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Julio",
           "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")
opciones <- c("L1","L2","L3","LA","LB","LC")
lugar <- list(L1=c("EXT","INT","LOC"), L2=c("NOM","KHA","POC"),
              L3= c("TMO", "MCR", "GNR"), LA=c("APV", "HOT", "ROR"),
              LB= c("GRU", "BOY", "POK"), LC=c("NOT", "LOX", "TAK"))

# UI定义
test_lugar <- tabPanel(
  title= "Test", 
  titlePanel(h5("Test")),
  sidebarLayout(    
    sidebarPanel(
      selectInput("año", "Año", choices=años, selected= "2019"), 
      selectInput("mes", "Mes", choices=meses, selected="Enero", multiple=TRUE),
      selectInput("opcion", "Opción", choices=opciones, selected="L1"),
      # 修正multiple参数位置
      selectInput("lugar", "Lugar", choices = c(), multiple=TRUE)
    ), 
    mainPanel(  
      plotlyOutput("afluencia_estaciones", height = "400px", width="1000px")
    )
  )
)

ui <- navbarPage(title = h5(strong("XXXX")),
                 theme = shinytheme("paper"),
                 test_lugar)

# 服务端逻辑
server <- function(input, output, session) {
  # 监听opcion变化,联动更新lugar选项
  observeEvent(input$opcion, {
    opciones_lugar <- lugar[[input$opcion]]
    updateSelectInput(session, "lugar", 
                      choices = opciones_lugar, 
                      selected = opciones_lugar[1])
  }, ignoreNULL = FALSE)
  
  # 柱状图绘制逻辑
  output$afluencia_estaciones <- renderPlotly({
    req(input$año, input$mes, input$opcion, input$lugar)
    df_filtrado <- df %>%
      filter(año == input$año,
             mes %in% input$mes,
             opciones == input$opcion,
             lugar %in% input$lugar)
    plot_ly(df_filtrado, x = ~lugar, y = ~numeros, type = "bar")
  })
}

# 启动应用
shinyApp(ui, server, options = list(launch.browser = TRUE))

内容的提问来源于stack exchange,提问作者Alejandro Carrera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 21:24:03