Shiny RStudio闪亮的代码可以逐行工作(Ctrl+;Enter),但不能使用;运行应用程序“;按钮

Shiny RStudio闪亮的代码可以逐行工作(Ctrl+;Enter),但不能使用;运行应用程序“;按钮,shiny,rstudio,run-app,Shiny,Rstudio,Run App,在RStudio中,如果我使用Ctrl+Enter,逐行运行下面闪亮的代码,那么它工作得很好。但是,如果我使用“run App”按钮运行整个代码,则会生成以下错误: ts(x)中的错误:“ts”对象必须有一个或多个观测值 我认为这是由于“lambda”参数,但我不明白为什么。感谢您的帮助 “data.csv”的链接是 ==================================== library(shiny) library(shinydashboard) library(plotly

在RStudio中,如果我使用Ctrl+Enter,逐行运行下面闪亮的代码,那么它工作得很好。但是,如果我使用“run App”按钮运行整个代码,则会生成以下错误:

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

我认为这是由于“lambda”参数,但我不明白为什么。感谢您的帮助

“data.csv”的链接是

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

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)
库(闪亮)
图书馆(shinydashboard)
图书馆(绘本)
图书馆(预测)
dfautoplot()返回ggplot对象。但是输出$forecast\u绘图需要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)
    })
}

ui单击“运行应用程序”按钮时,我不确定您是否“运行整个代码”。RStudio是否不仅考虑了ui()和server()函数?试着在ui()上面有一个包含代码的global.R,并有两个文件ui.R和server.R,而不是一个文件;你解决了问题!谢谢。我进一步试验了相同的代码(如您所建议的,使用global.R、ui.R、server.R),并将3行代码块移动到shinyServer中。它产生了一个错误。但是,如果我替换“lbdHi tasasaki”,谢谢你的回答。当我运行我的代码时,我观察到的情况如下。我的代码运行没有任何问题,除非我按下“运行应用程序”“按钮!”警告:包“plotly”是在R版本3.5.3下生成的加载所需包:ggplot2警告:包“ggplot2”是在R版本3.5.2下生成的附加包:“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))
    })
}
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)