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

Shiny中使用Select Input调用autoplot绘图出现直线显示异常问题

代码问题修复说明

问题根源

input$select返回的是字符串类型的变量名,而适配tsibble对象的autoplot()方法遵循tidyverse非标准求值规则,需要传入裸变量名而非字符串,直接传入字符串会被识别为固定常量,因此只会绘制一条水平线,只有Y轴标签能正常读取字符串内容完成显示。

修复后完整代码

推荐使用.data代词承接字符串格式的变量名,修改后代码可正常绘图:

library(shiny)
library(fpp3)
ui <- fluidPage(
  selectInput("select", "Choose variable", choices = names(aus_production)),
  plotOutput("plot")
)

server <- function(input,output){
  output$plot <- renderPlot({
   aus_production %>% autoplot(.data[[input$select]]) 
  })
}

shinyApp(ui = ui,server = server)

可选替代写法

你也可以通过符号转换注入的方式实现相同效果,写法如下:

aus_production %>% autoplot(!!rlang::sym(input$select))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 08:24:03