Error When Running Shiny App: the condition has length > 1

This is the first time I write on here, so please excuse me if I do not use the proper format. I was trying to create an interactive forecasting application using Shiny.
Whenever I try to run the app, I get the following error: the condition has length > 1.

I tried to include the dataset I am using for this app but it seems that I am not allowed to attach it.
What could be causing this error?

Here is my ui.R code:
#install.packages("plyr")
#install.packages("rintrojs")
#install.packages("DT")
#install.packages("forecast")

#loading required packages
library(readxl)
library(plotly)
library(shinycssloaders)
library(shinythemes)
library(shinydashboard)
library(plyr)
library(rintrojs)
library(DT)
library(forecast)

#defining working directory
setwd("C:/Users/paulbernal/Desktop/Project 04")

#importing dataset for interactive visualization
df1 <- read.csv("dataset01.csv")

shinyUI(fluidPage(theme = shinytheme("cerulean"),

tabPanel("Forecasting",
fluidRow(
sidebarLayout(
sidebarPanel(
tabsetPanel(id = "tabs",
tabPanel("Metric Forecast", value = "Tab1",
selectInput("Metric", "Metric:",
choices = colnames(df1[2:49]),
hr(),
helpText("choose metric above to forecast")))),
mainPanel(
plotlyOutput("Plot1")
)

))))))

This is my server.R code:
#installing required R packages for shiny app
#install.packages("readxl")
#install.packages("plotly")
#install.packages("shiny")
#install.packages("shinycssloaders")
#install.packages("shinythemes")
#install.packages("shinydashboard")
#install.packages("plyr")
#install.packages("rintrojs")
#install.packages("DT")
#install.packages("forecast")

#loading required packages
library(readxl)
library(plotly)
library(shinycssloaders)
library(shinythemes)
library(shinydashboard)
library(plyr)
library(rintrojs)
library(DT)
library(forecast)

#defining working directory
setwd("C:/Users/paulbernal/Desktop/Project 04")

#importing dataset for interactive visualization
df1 <- read.csv("dataset01.csv")

#create forecast dates
fcast_dates <- (2021:2027)

server <- function(input, output) {

  observeEvent(input$Metric, {
    
      output$plot1 <- renderPlotly({
        plot_ly(x = df1$Year, y = df1[,colnames(df1) == input$Metric], name = 'Historical',
                type = 'scatter', mode = 'lines', line = list(color = 'rgb(205, 12, 24', width = 4))%>%
          add_trace(x = fcast_dates, y = forecast(auto.arima(df1[,colnames(df1) == input$Metric]), h = 7)$mean,
                    name = 'Forecast', line = list(color = 'rgb(22, 96, 167)', diwth = 4, dash = 'dot'))%>%
          layout(title = "Non-Seasonal Forecast)",
                 xaxis = list(title = "Years"),
                 yaxis = list(title = "Selected Metric"))
                 
      })})}

To better help troubleshoot, can you please share what you get when executing the following? This will provide a look at the data structure you're working with.

dplyr::glimpse(df1)

Hi Scott,

This is what I get when I run dplyr::glimpse(df1)

I noticed a couple parentheses out of place in the UI section, so I reformatted below and added comments on closing parentheses. After making these updates, the error was resolved.

shinyUI(
  fluidPage(
    theme = shinytheme("cerulean"),
    tabPanel("Forecasting",
             fluidRow(
               sidebarLayout(
                 sidebarPanel(
                   tabsetPanel(id = "tabs",
                               tabPanel("Metric Forecast", 
                                        value = "Tab1",
                                        selectInput("Metric", 
                                                    "Metric:",
                                                    choices = colnames(df1[2:4])
                                                    ), 
                                        hr(),
                                        helpText("choose metric above to forecast")
                                        ) # closes tabPanel
                               ) # closes tabsetPanel
                   ), # closes sidebarPanel
                 mainPanel(
                   plotlyOutput("Plot1")
                   )
                 ) # closes sidebarLayout
               ) # closes fluidRow
             ) # closes tabPanel
    ) # closes fluidPage
  ) # closes shinyUI

image

Hi Scott,

Thank you so much for your valuable feedback. For some reason, once the page pops up the Shiny application stops, why could this be happening?

Does an error show up in the console? If so, can you share it?

Hi Scott,

I can now see the dashboard, however, the forecasts do not appear. Which is odd.

This is my ui.R code:
#loading required packages
library(readxl)
library(plotly)
library(shinycssloaders)
library(shinythemes)
library(shinydashboard)
library(plyr)
library(rintrojs)
library(DT)
library(forecast)

#defining working directory
setwd("C:/Users/paulbernal/Desktop/Project 04")

#importing dataset for interactive visualization
df1 <- read.csv("dataset01.csv")

shinyUI(
fluidPage(
theme = shinytheme("cerulean"),
tabPanel("Forecasting",
fluidRow(
sidebarLayout(
sidebarPanel(
tabsetPanel(id = "tabs",
tabPanel("Metric Forecast",
value = "Tab1",
selectInput("Metric",
"Metric:",
choices = colnames(df1[2:49]),
),
hr(),
helpText("choose metric above to forecast")
) # closes tabPanel
) # closes tabsetPanel
), # closes sidebarPanel
mainPanel(
plotlyOutput("Plot1")
)
) # closes sidebarLayout
) # closes fluidRow
) # closes tabPanel
) # closes fluidPage
) # closes shinyUI

And this is my server.R code:
#loading required packages
library(readxl)
library(plotly)
library(shinycssloaders)
library(shinythemes)
library(shinydashboard)
library(plyr)
library(rintrojs)
library(DT)
library(forecast)

#defining working directory
setwd("C:/Users/paulbernal/Desktop/Project 04")

#importing dataset for interactive visualization
df1 <- read.csv("dataset01.csv")

#create forecast dates
fcast_dates <- (2021:2027)

server <- function(input, output) {

  observeEvent(input$Metric, {
    
      output$plot1 <- renderPlotly({
        plot_ly(x = df1$Year, y = df1[,colnames(df1) == input$Metric], name = 'Historical',
                type = 'scatter', mode = 'lines', line = list(color = 'rgb(205, 12, 24', width = 4))%>%
          add_trace(x = fcast_dates, y = forecast(auto.arima(df1[,colnames(df1) == input$Metric]), h = 7)$mean,
                    name = 'Forecast', line = list(color = 'rgb(22, 96, 167)', diwth = 4, dash = 'dot'))%>%
          layout(title = "Non-Seasonal Forecast)",
                 xaxis = list(title = "Years"),
                 yaxis = list(title = "Selected Metric"))
                 
      })})}


You can see the selection menu, but no forecast is shown.

Best,
Paul

In the UI section, change Plot1 to lowercase to match the output in the server.

plotlyOutput("plot1")

Also, since input$Metric is within your plotly code, the plot will update each time a new metric is selected. Therefore, you do not need to wrap the output in an observeEvent().

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.