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

Shiny代码报'attempt to apply non-function'错误,请求排查语法问题

问题翻译

我运行下面的Shiny代码时出现了attempt to apply non-function错误,但找不到语法问题。这是我编程课程的期末项目,现在因为这个错误没法继续,麻烦帮忙排查代码问题。

shinyUI(
  fluidPage(
    br(),
    
    # TASK 1: Application title
    titlePanel(title = "Trends in Demographics and Income"),
    p("Explore the difference between people who earn less than 50K and more than 50K. You can filter the data by country, then explore various demographic information."),
    
    # TASK 2: Add first fluidRow to select input for country
    fluidRow(
      column(12, 
             wellPanel(varSelectInput("country",
                                      "Country of Origin",
                                      data = adult, 
                                      selected = "United-States"))  # add select input 
             )),
    
    # TASK 3: Add second fluidRow to control how to plot the continuous variables
    fluidRow(
      column(3, 
             wellPanel(
               p("Select a continuous variable and graph type (histogram or boxplot) to view on the right."),
               radioButtons("continuous_variable",
                            label = "Age or Hrs/wk",
                            choices = c("age", "hour_per_week"),
                            selected = "age"),                  # add radio buttons for continuous variables
               radioButtons("graph_type",
                            label = "Histogram or Boxplot",
                            choices = c("histogram", "boxplot"),
                            selected = "histogram")             # add radio buttons for chart type
               )),
      column(9, plotOutput("p1"))  # add plot output
            ),
    
    # TASK 4: Add third fluidRow to control how to plot the categorical variables
    fluidRow(
      column(3, 
             wellPanel(
               p("Select a categorical variable to view bar chart on the right. Use the check box to view a stacked bar chart to combine the income levels into one graph. "),
               radioButtons("categorical_variable",
                            "Categorical Variables",
                            choices = c("Education","Workclass","Sex"),
                            selected = "Education"),    # add radio buttons for categorical variables
               checkboxInput("is_stacked",
                             label = "Stacked",
                             value = FALSE)     # add check box input for stacked bar chart option
               )
             ),
      column(9, plotOutput("p2"))  # add plot output
    )
  )
)

排查出的关键问题及修复方案

1. 组件使用错误:varSelectInput误用为取值选择器

varSelectInput的作用是让用户选择数据框中的列,但你需要的是选择country列的具体取值(比如United-States),这时候应该用selectInput:

# 替换原varSelectInput代码
selectInput("country",
            "Country of Origin",
            choices = unique(adult$country),  # 提取country列的所有唯一取值
            selected = "United-States")

2. HTML转义引号导致语法错误

原代码中的"是HTML的双引号转义字符,R无法识别,必须替换为正常的双引号"(已在上面的代码示例中修正)。

3. 分类变量选项大小写不匹配

R对大小写敏感,你写的"Education","Workclass","Sex"要和adult数据中的列名完全一致。如果数据里的列名是小写的education/workclass/sex,就会导致变量找不到的错误,建议改成匹配数据的写法。

4. 服务器端(未提供)潜在的变量引用错误

如果你的server代码中直接用input$continuous_variable作为ggplot的映射变量,会因为字符串无法直接作为变量名报错。需要用.data代词来引用字符串形式的变量,示例如下:

output$p1 <- renderPlot({
  filtered_data <- adult %>% filter(country == input$country)
  p <- ggplot(filtered_data, aes(x = .data[[input$continuous_variable]], fill = income))
  if(input$graph_type == "histogram"){
    p + geom_histogram(bins = 30, position = "dodge")
  } else {
    p + geom_boxplot()
  }
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 14:18:16