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

Shiny应用报错:checkName错误——索引reactivevalues需用单个字符串

Shiny应用报错排查:Error in checkName: Must use single string to index into reactivevalues.

我正在开发一款日志记录Shiny应用,功能是接收用户输入并追加到展示表格中。概念验证版运行正常,但功能完善版启动后报错:Error in checkName: Must use single string to index into reactivevalues.

正常运行的概念验证版app.R

library(shiny)

ui <- fluidPage(
  sidebarPanel(
    selectInput("data_name", "Select data frame name:", choices = c("dataTable1", "dataTable2")),
    textInput("name", "Enter your name:", ""),
    numericInput("age", "Enter your age:", ""),
    actionButton("submit", "Submit")
  ),
  mainPanel(
    tableOutput("input_table")
  )
)

server <- function(input, output) {
  data <- reactiveValues(
    dataTable1 = data.frame(Name = character(), Age = numeric()),
    dataTable2 = data.frame(Name = character(), Age = numeric())
  )
  
  observeEvent(input$submit, {
    new_entry <- data.frame(Name = input$name, Age = input$age)
    data_name <- input$data_name
    data[[data_name]] <- rbind(data[[data_name]], new_entry)
  })
  
  output$input_table <- renderTable({
    data[[input$data_name]]
  })
}

shinyApp(ui = ui, server = server)

报错的完整版app.R

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("Operation", label = "Operation", choices = c("ENiG", "Autocatalytic_Sn", "Electroless_Cu", "Acid_Cu", "SnPb")),
      dateInput("Date", label = "Date"),
      textInput("Customer", label = "Customer", value = ""),
      textInput("Job", label = "Job Number", value = ""),
      selectInput("Size", label = "Panel Size", choices = c("18x12", "18x24")),
      numericInput("QTY", label = "Quantity", value = 1),
      numericInput("Load", label = "Load Count", value = 1),
      selectInput("Surf", label = "surface Type", choices = c("Circuit Pattern", "Full Body", "Pads", "sheild", "Tabs")),
      numericInput("Time", label = "Plating time... min", value = 1),
      textInput("Operator", label = "Operator... Init/Name", value = ""),
      textAreaInput("Comments", label = "Comments & Notes", value = ""),
      actionButton("Submit", label = "Add Entry")
    ),
    mainPanel(
      tableOutput("input_table")
    ),
    position = c("right")
  )
)  

server <- function(input, output) {
  data <- reactiveValues(
    ENiG = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                      Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                      Time = numeric(), Operator = character(), Comments = character()),
    Autocatalytic_Sn = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                                  Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                                  Time = numeric(), Operator = character(), Comments = character()),
    Electroless_Cu = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                                Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                                Time = numeric(), Operator = character(), Comments = character()),
    Acid_Cu = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                         Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                         Time = numeric(), Operator = character(), Comments = character()),
    SnPb = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                      Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                      Time = numeric(), Operator = character(), Comments = character())
  )
  
  observeEvent(input$submit, {
    new_entry <- data.frame(Operation = input$Operation , Date = input$Date, Customer = input$Customer,
                            Job = input$Job, Size = input$Size, QTY = input$QTY,
                            Load = input$Load, Surf = input$Surf, Time = input$Time,
                            Operator = input$Operator, Comments = input$Comments)
    Log_Name <- input$Operation
    data[[Log_Name]] <- rbind(data[[Log_Name]], new_entry)
  })
  
  output$input_table <- renderTable({
    data[[input$Log_Name]]
  })
}

shinyApp(ui = ui, server = server)

问题排查与解决

报错的核心原因是两处输入ID不匹配:

  1. 按钮ID大小写不匹配
    UI中定义的提交按钮ID是Submit(大写S):actionButton("Submit", label = "Add Entry"),但server中监听的是input$submit(小写s)。Shiny的输入ID是大小写敏感的,导致observeEvent从未触发,后续的Log_Name变量也无法被正确赋值,进而引发索引错误。

  2. 表格渲染时的输入ID错误
    在renderTable中使用了input$Log_Name,但UI中并没有名为Log_Name的输入控件,应该替换为用户实际选择的操作控件IDinput$Operation。

修正后的server代码

server <- function(input, output) {
  data <- reactiveValues(
    ENiG = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                      Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                      Time = numeric(), Operator = character(), Comments = character()),
    Autocatalytic_Sn = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                                  Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                                  Time = numeric(), Operator = character(), Comments = character()),
    Electroless_Cu = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                                Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                                Time = numeric(), Operator = character(), Comments = character()),
    Acid_Cu = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                         Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                         Time = numeric(), Operator = character(), Comments = character()),
    SnPb = data.frame(Operation = character(), Date = character(), Customer = character(), Job = character(),
                      Size = character(), QTY = numeric(), Load = numeric(), Surf = character(),
                      Time = numeric(), Operator = character(), Comments = character())
  )
  
  # 修正按钮ID为大写Submit
  observeEvent(input$Submit, {
    new_entry <- data.frame(Operation = input$Operation , Date = input$Date, Customer = input$Customer,
                            Job = input$Job, Size = input$Size, QTY = input$QTY,
                            Load = input$Load, Surf = input$Surf, Time = input$Time,
                            Operator = input$Operator, Comments = input$Comments)
    Log_Name <- input$Operation
    data[[Log_Name]] <- rbind(data[[Log_Name]], new_entry)
  })
  
  # 修正为正确的输入ID input$Operation
  output$input_table <- renderTable({
    data[[input$Operation]]
  })
}

内容的提问来源于stack exchange,提问作者Logan Gaynor

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 06:40:42