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

部署Shiny应用至shinyapps.io时遭遇filter函数适配错误求助

Shiny部署shinyapps.io遇filter函数错误的排查与解决

问题描述

部署Shiny应用至shinyapps.io时,触发以下错误:

Warning: Error in UseMethod: no applicable method for 'filter' applied to an object of class "function"

应用在本地运行完全正常,已在UI脚本中加载dplyr包,且尝试在server脚本中显式调用dplyr::filter,但错误仍未消除。

应用代码示例

注:实际部署时UI与server为独立脚本,server脚本末尾无shinyApp()

library(lubridate)
library(dplyr)
library(tidyr)
library(ggplot2)
library(shiny)
library(shinydashboard)

# 问题根源:数据集命名与base R的data()函数重名
data <- tibble(
  site = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"),
  date = as.Date(c("2005-06-15", "2005-06-15", "2006-06-15", "2006-06-15", "2007-06-15", "2007-06-15", "2008-06-15", "2008-06-15", "2009-06-15", "2009-06-15", "2005-06-15", "2005-06-15", "2006-06-15", "2006-06-15", "2007-06-15", "2007-06-15", "2008-06-15", "2008-06-15", "2009-06-15", "2009-06-15")),
  Variable = c("temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph", "temperature", "ph"),
  Value = c(20.9, 7.27, 20.8, 6.98, 20.1, 7.08, 18.0, 6.99, 20.6, 6.72, 19.9, 6.92, 19.8, 6.92, 18.5, 6.99, 19.5, 7.22, 20.4, 7.15)
)

ui <- dashboardPage(
  dashboardHeader(title = "Time Series Viewer"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Time Series", tabName = "time_series", icon = icon("chart-line"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "time_series",
        fluidRow(
          box(
            title = "Time Series",
            status = "primary",
            solidHeader = TRUE,
            width = 2,
            selectInput("siteInput", "Select Site", choices = unique(data$site)),
            selectInput("variableInput", "Select Variable", choices = unique(data$Variable))
          ),
          box(
            title = "Plot",
            status = "primary",
            solidHeader = TRUE,
            width = 10,
            plotOutput("timeSeriesPlot", height = "700px")
          )
        )
      )
    )
  )
)

# Define server
server <- function(input, output) {
  output$timeSeriesPlot <- renderPlot({
    filteredData <- data %>%
      filter(site == input$siteInput, Variable == input$variableInput)
    
    variableName <- unique(filteredData$Variable)
    yLabel <- ifelse(variableName == "temperature", expression(Water~Temperature~(degree*C)),variableName)
    
    ggplot(filteredData, aes(x = date, y = Value)) +
      geom_line() +
      geom_point() +
      labs(x = "", y = yLabel) +
      scale_x_date(date_labels = "%b %Y", date_breaks = "1 year") +
      theme_bw() +
      theme(text = element_text(size = 16),
            axis.text.x = element_text(size = 16, color = "black", angle = 90, hjust = 1, vjust = 0.5),
            axis.text.y = element_text(size = 16, color = "black")) +
      ggtitle(paste("Site:", input$siteInput))
  })
}

# Run the application
shinyApp(ui = ui, server = server)

部署日志核心片段

2023-06-29T14:21:54.880132+00:00 shinyapps[9384275]: Warning: Error in UseMethod: no applicable method for 'filter' applied to an object of class "function"
2023-06-29T14:21:54.912458+00:00 shinyapps[9384275]:   184: filter
2023-06-29T14:21:54.912485+00:00 shinyapps[9384275]:   183: %>%
2023-06-29T14:21:54.912489+00:00 shinyapps[9384275]:   182: renderPlot [/srv/connect/apps/test/server.R#3]

更新说明

已尝试在server中使用dplyr::filter,但错误依然存在,日志中仅将报错行改为184: dplyr::filter。


解决方法

问题根源在于数据集变量名data与base R中的内置函数data()重名。本地环境中用户定义的data数据框优先级较高,但在shinyapps.io部署环境中,变量名解析优先级发生变化,导致data被识别为函数而非数据框,因此调用filter时会出现“应用到function类对象”的错误。

修复步骤:

  • 将所有脚本中的数据集变量名从data改为其他不与内置函数重名的名称,比如app_data;
  • 替换所有引用该数据集的位置,包括UI中的unique(data$site)、unique(data$Variable),以及server中的data %>% filter(...);
  • 重新部署应用。

修改后的核心代码示例:

# 重命名数据集
app_data <- tibble(
  # 数据内容不变
)

# UI中替换引用
selectInput("siteInput", "Select Site", choices = unique(app_data$site)),
selectInput("variableInput", "Select Variable", choices = unique(app_data$Variable))

# Server中替换引用
filteredData <- app_data %>%
  dplyr::filter(site == input$siteInput, Variable == input$variableInput)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 12:29:51