R Shiny调用ggstatsplot绘图报Can't convert to a symbol错误
问题描述
在R Shiny应用中使用ggstatsplot包绘制箱线小提琴图时,运行抛出Can't convert to a symbol.报错。已尝试将输入参数按符号格式传入仍未解决,经测试数据集本身无异常,脱离Shiny环境单独运行绘图代码可正常出图。
附:相关代码与测试数据
UI端代码
library(ggstatsplot) library(shiny) total_data <- read_rds('data/total_data.rds') ui <- fluidPage( titlePanel("Trial"), sidebarLayout( sidebarPanel( selectInput(inputId = "Weekday", label = "Choose a Weekday Type", choices = c( "Weekday Earnings" = "weekday_earn", "Weekend Earnings" = "weekend_earn", "Total Earnings" = "total_earn" ), selected = "total_earn") ), mainPanel( plotOutput("distPlot") ) ) )
Server端代码
server <- function(input, output) { output$distPlot <- renderPlot({ ggbetweenstats( data = total_data, x = type, y = .data[[input$Weekday]], type = "p", xlab = "Business Type", ylab = "Revenue", palette = "Set3", ggtheme = ggplot2::theme_gray() ) }) } shinyApp(ui = ui, server = server)
测试数据集dput输出
structure(list(Id = c(445, 446, 447, 448, 449, 895, 896, 897, 898, 899, 1345, 1346, 1347, 1348, 1349, 1801, 1802, 1803, 1804, 1805, 442, 443, 444, 892, 893, 894, 1342, 1343, 1344, 1798, 1799, 1800), weekday_earn = c(31317.15, 55244.16, 138308.94, 103455.33, 143642.1, 82046.76, 95939.92, 98409.6, 48108, 125085.35, 66392.2, 28863.9, 37467.36, 22592.43, 20925.99, 211606.04, 63408.56, 33355.8, 40972.32, 157498.25, 109567.27, 86654.63, 124132.1, 162232.24, 202706.02, 133736.67, 363413.08, 186544.61, 309771.65, 229891.03, 143759.45, 201677.06), weekend_earn = c(11726.55, 19344.63, 62996.84, 31286.09, 69409.13, 52711.68, 25854.32, 81682.56, 5912.22, 38165.75, 57311.8, 8137.35, 4282.11, 18112.86, 3286.53, 54284.52, 18079.72, 19357.5, 51800, 61501.18, 128365.38, 94894.62, 127113.99, 189662.93, 251234.36, 176850.53, 417060.38, 240337.75, 369086.74, 230502.94, 159370.02, 228480.24), total_earn = c(43043.7, 74588.79, 201305.78, 134741.42, 213051.23, 134758.44, 121794.24, 180092.16, 54020.22, 163251.1, 123704, 37001.25, 41749.47, 40705.29, 24212.52, 265890.56, 81488.28, 52713.3, 92772.32, 218999.43, 237932.65, 181549.25, 251246.09, 351895.17, 453940.38, 310587.2, 780473.46, 426882.36, 678858.39, 460393.97, 303129.47, 430157.3), type = c("Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Restaurant", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub", "Pub")), row.names = c(NA, -32L), class = c("tbl_df", "tbl", "data.frame"))
报错原因
报错根源是ggbetweenstats的非标准计算逻辑与Shiny响应式执行环境存在兼容冲突:
x参数传入裸列名type时,函数内部尝试将参数转换为符号的过程中,无法在Shiny的封闭执行环境中定位到该裸名对应的数据集列- 旧版本
ggstatsplot对.data[[ ]]形式的动态列名解析存在缺陷,在Shiny环境中无法正确识别代词指向的列,最终触发转符号失败错误。
修复方案
提供两种可直接生效的修改方式:
方案1:统一.data代词封装(推荐)
将所有裸列名参数统一用.data代词封装,保证x、y参数的解析逻辑一致,适配Shiny执行环境:
server <- function(input, output) { output$distPlot <- renderPlot({ ggbetweenstats( data = total_data, x = .data[["type"]], y = .data[[input$Weekday]], type = "p", xlab = "Business Type", ylab = "Revenue", palette = "Set3", ggtheme = ggplot2::theme_gray() ) }) }
方案2:字符串传参(兼容旧版本)
如果升级ggstatsplot后方案1仍不生效,可直接传入列名字符串,完全规避非标准计算的环境定位问题,ggbetweenstats原生支持该传参形式:
server <- function(input, output) { output$distPlot <- renderPlot({ ggbetweenstats( data = total_data, x = "type", y = input$Weekday, type = "p", xlab = "Business Type", ylab = "Revenue", palette = "Set3", ggtheme = ggplot2::theme_gray() ) }) }
提示:若运行时提示找不到
read_rds函数,提前安装加载readr包即可,与本次报错无关。
内容的提问来源于stack exchange,提问作者Jordan Iatro
相关产品推荐
相关产品推荐

