【问题标题】:RStudio-Shiny code works line-by-line (Ctrl+Enter), but not with the "Run App" buttonRStudio-Shiny 代码逐行工作(Ctrl+Enter),但不能使用“运行应用程序”按钮
【发布时间】:2019-10-08 07:33:58
【问题描述】:

在 RStudio 中,如果我使用 Ctrl+Enter 逐行运行下面的 Shiny 代码,它可以正常工作。但是,如果我使用“运行应用程序”按钮运行整个代码,则会生成此错误:

ts(x) 中的错误:“ts”对象必须有一个或多个观察结果

我认为这是由于“lambda”参数,但我不明白为什么。任何帮助表示赞赏。

“data.csv”的链接是https://www.dropbox.com/s/p1bhacdg8j1qx42/data.csv?dl=0

======================================

library(shiny)
library(shinydashboard)
library(plotly)
library(forecast)

df <- read.csv("data.csv")
demand <- ts(df$demand, start = c(1995, 1), frequency = 12)

lbd <- BoxCox.lambda(demand, lower=-5, upper=5)
m <- ar(BoxCox(demand,lambda=lbd))
fit_BC <- forecast(m, h=12, lambda=lbd)

ui <- dashboardPage(
  dashboardHeader(title = "Plot"),
  dashboardSidebar(disable = TRUE),
  dashboardBody(fluidRow(column(width = 12, box(plotlyOutput("forecast_plots"),width = NULL))))
)

server <- function(input, output) {
  output$forecast_plots <- renderPlotly({
    autoplot(fit_BC)
  })
}

shinyApp(ui, server)

===================================

【问题讨论】:

  • 我不确定您在单击“运行应用程序”按钮时是否“运行了整个代码”。 RStudio 不仅考虑您的 ui() 和 server() 功能吗?尝试使用包含 ui() 上方代码的 global.R 并使用两个文件 ui.R 和 server.R 而不是单个文件。
  • 嗨@tic-toc-choc,太好了;你解决了问题!非常感谢。我进一步试验了相同的代码(如你所建议的使用 global.R、ui.R、server.R)并将 3 行块移动到 shinyServer。它产生了一个错误。但是,如果我将“lbd

标签: shiny rstudio run-app


【解决方案1】:

autoplot() 返回 ggplot 对象。但是你的 output$forecast_plots 需要 plotly 对象(带有 plotlyOutput() 函数)。

工作代码如下:

ui <- dashboardPage(
    dashboardHeader(title = "Plot"),
    dashboardSidebar(disable = TRUE),
    dashboardBody(fluidRow(column(width = 12, box(plotOutput("forecast_plots"),width = NULL))))
)

server <- function(input, output) {
    output$forecast_plots <- renderPlot({
        autoplot(fit_BC)
    })
}

ggplot 对象可以使用 ggplotly 函数轻松转换,但不幸的是,转换后的 plotly 自动绘图图会丢失预测区域。您可以像这样验证它:

ui <- dashboardPage(
    dashboardHeader(title = "Plot"),
    dashboardSidebar(disable = TRUE),
    dashboardBody(fluidRow(column(width = 12, box(plotlyOutput("forecast_plots"),width = NULL))))
)

server <- function(input, output) {
    output$forecast_plots <- renderPlotly({
        ggplotly(autoplot(fit_BC))
    })
}

添加

我找到了自动绘图库。https://terrytangyuan.github.io/2018/02/12/autoplotly-intro/

autoplotly() 函数可以将自动绘图对象转换为大致正确的绘图对象。

library(shiny)
library(shinydashboard)
library(plotly)
library(forecast)
library(autoplotly)

df <- read.csv("c:/Users/010170283/Downloads/data.csv")
demand <- ts(df$demand, start = c(1995, 1), frequency = 12)

lbd <- BoxCox.lambda(demand, lower=-5, upper=5)
m <- ar(BoxCox(demand,lambda=lbd))
fit_BC <- forecast(m, h=12, lambda=lbd)

ui <- dashboardPage(
    dashboardHeader(title = "Plot"),
    dashboardSidebar(disable = TRUE),
    dashboardBody(fluidRow(column(width = 12, box(plotlyOutput("forecast_plots"),width = NULL))))
)

server <- function(input, output) {
    output$forecast_plots <- renderPlotly({
        autoplotly(autoplot(fit_BC))
    })
}

shinyApp(ui, server)

使用它可以看到预测区域,并且通过鼠标悬停事件呈现高/低 80 % 边缘值。

【讨论】:

  • 您好 tasasaki,感谢您的回答。当我运行我的代码时,我观察到的内容如下。我的代码运行没有任何问题,除非我按下“运行应用程序”按钮!:警告:包'plotly'是在 R 版本 3.5.3 下构建的加载所需的包:ggplot2 警告:包'ggplot2'是在 R 版本 3.5 下构建的。 2 附加包:'plotly'
猜你喜欢
  • 1970-01-01
  • 2017-11-16
  • 2019-09-28
  • 1970-01-01
  • 2018-02-16
  • 2023-04-03
  • 2019-03-20
  • 1970-01-01
  • 2018-02-13
相关资源
最近更新 更多