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

R Shiny sidebarPanel出现Undefined columns selected错误求助

错误根因

  • 反应式数据调用错误:你定义的df_products_upload是反应式对象,必须加括号df_products_upload()才能获取到实际的数据集,你在三个输出渲染函数里直接用未定义的df变量、或者不带括号调用df_products_upload,都无法拿到有效数据。
  • 冗余的下拉框更新逻辑:你已经通过observeEvent在文件上传后把下拉框的选项更新为数据集的列名,不需要在每个render*函数里重复调用updateSelectInput,会触发不必要的响应循环。
  • 列选择逻辑错误:下拉框选项更新为列名(字符串格式)后,你还用as.numeric(input$Variable)转换列名,会得到NA值,自然无法选中有效列,触发"Undefined columns selected"报错。
  • 语法错误:output$sum的代码中缺少右括号,逻辑不闭合。

修正后可运行的完整代码

UI部分

shinyUI(fluidPage(
  main_page <- tabPanel(
    title = "Statistics",
    titlePanel("Statistical Analysis"),
    
    sidebarLayout(
      sidebarPanel(
        fileInput('DDBB', 'Choose file to upload',
                  accept = c(
                    'text/csv',
                    'text/comma-separated-values',
                    '.csv'
                  )),
        selectInput('Variable', 'Select a variable', choices = NULL,
                    multiple = FALSE,
                    selectize = TRUE
        ),
        sliderInput("bins",
                    "Number of bins:",
                    min = 1,
                    max = 200,
                    value = 30)
      ),
      
      mainPanel(
        tabsetPanel(
          tabPanel(
            title = "Data Visualization",
            DT::dataTableOutput("sample_table")
          ),
          tabPanel(
            title = "Summary Statistics",
            verbatimTextOutput("sum"),
            fluidRow(splitLayout(cellWidths = c("50%", "50%"),
                                 plotOutput("box"),
                                 plotOutput('hist'))
            )
          )
        )
      )
    )
  )
))

Server部分

shinyServer(function(input, output, session) {
  
  df_products_upload <- reactive({ 
    inFile <- input$DDBB 
    if (is.null(inFile))
      return(NULL)
    df <- read.csv(inFile$datapath, header = TRUE,sep = ';')
    return(df)
  })
  
  output$sample_table<- DT::renderDataTable({ 
    df <- req(df_products_upload()) 
    DT::datatable(df,
                  filter = 'top') %>%
      formatRound(columns= c(1:ncol(df)), digits = 2)
  })
  
  observeEvent(df_products_upload(), {
    updateSelectInput(session, "Variable", choices = colnames(df_products_upload()))
  })
  
  output$sum <- renderPrint({
    df <- req(df_products_upload())
    summary(df[[input$Variable]])
  })
  
  output$box <- renderPlot({
    df <- req(df_products_upload())
    x <- df[[input$Variable]]
    boxplot(x,col="sky blue",border="purple",main=input$Variable)
  })
  
  output$hist <- renderPlot({
    df <- req(df_products_upload())
    x <- df[[input$Variable]]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, 
         main=input$Variable,
         breaks = bins, col = 'darkgray', border = 'black',
         xlab = 'samples')
  })
  
})

关键修正说明

  1. 用req()函数校验数据是否上传完成,避免文件未上传时出现不必要的报错。
  2. 下拉框选择变量后直接用列名取数,不需要转数值,df[[input$Variable]]可以直接拿到对应列的数值。
  3. 图表标题直接用input$Variable即可显示选中的变量名,不需要再从数据集里取列名。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 06:45:02