problems to include table with the forecasted values and an interactive plot with forecasts in Shiny using fpp3/forecast

I am trying to get a Shiny app with forecasts using the fpp3/forecast library.

I would like to show the mean monthly forecasts and the confifence intervals values in a table, but I can not get the output in Shiny. Here, I am using an example from the fpp3 book, which in my case works well in RStudio, but it does not work in a Shiny app. This is just a basic Shiny example to create a table (using shinycssloaders loads an animation while the model is still fitting and forecasting, and when the results are ready the animation disappears and the ouputs/results are showed).

library(shiny)
library(fpp3)
library(shinycssloaders)
library(DT)



ui <- fluidPage(
 
 withSpinner(tableOutput("table"))
# DT::dataTableOutput("table")
)

server <- function(input, output) {

leisure <- us_employment %>%
  filter(Title == "Leisure and Hospitality",
         year(Month) > 2000) %>%
  mutate(Employed = Employed/1000) %>%
  select(Month, Employed)
autoplot(leisure, Employed) +
  labs(title = "US employment: leisure and hospitality",
       y="Number of people (millions)")
	   
	   fit <- leisure %>%
  model(
  auto = ARIMA(Employed, stepwise = FALSE, approx = FALSE)
  )
  
  fc<-forecast(fit, h=36) 

 
  output$table <- renderTable({
# output$table <- DT::renderDataTable({
    data<-fc %>% hilo() %>%  select(.model,Month,.mean)
    
  })
}

shinyApp(ui, server)
	 

In the renderTable section using data<-fc %>% hilo() does not work.
But using data<-fc %>% hilo()%>% select(.model,Month,.mean) gives a Table but only with the forecasted means and not with the confidence intervals values. In addition, I would need the Month variable with a different format to show information about month and year. How is it possible to include the values for the confidence intervals (80% and 95%) and the Month variable with the correct format? Is there a different way to show the forecasted mean and the confidence intervals in a Table in Shiny?

Using data<-fc %>% hilo()%>% select(.model,Month,.mean,80%,95%) gives and error and it does not select the information for the 80% and 95% confidence intervals.

I have tried both static table and dynamic table (with comented lines using DT), but it is not working. Could you please check and let me know how to solve this issue?

Another question that I have is related with interactive plots for the forecast. When I create a static plot of the forecast it works perfectly with the following code:

library(shiny)
library(fpp3)
library(shinycssloaders)

ui <- fluidPage(
 
 withSpinner(plotOutput("plot"))

)

server <- function(input, output) {

leisure <- us_employment %>%
  filter(Title == "Leisure and Hospitality",
         year(Month) > 2000) %>%
  mutate(Employed = Employed/1000) %>%
  select(Month, Employed)
autoplot(leisure, Employed) +
  labs(title = "US employment: leisure and hospitality",
       y="Number of people (millions)")
	   
	   fit <- leisure %>%
  model(
  auto = ARIMA(Employed, stepwise = FALSE, approx = FALSE)
  )
  
  fc<-forecast(fit, h=36) 

 
  output$plot <- renderPlot({
    plot<-fc %>% autoplot(leisure) +
  labs(title = "US employment: leisure and hospitality",
       y="Number of people (millions)")
    plot
  })
}

shinyApp(ui, server)

However, when I want to create a dynamic plot of the forecasts it does not work, as it only shows the data, but not the forecasts in the dynamic plot using the following code:

library(shiny)
library(fpp3)
library(shinycssloaders)
librray(plotly)

ui <- fluidPage(
 
 withSpinner(plotlyOutput("plot"))
)

server <- function(input, output) {

leisure <- us_employment %>%
  filter(Title == "Leisure and Hospitality",
         year(Month) > 2000) %>%
  mutate(Employed = Employed/1000) %>%
  select(Month, Employed)
autoplot(leisure, Employed) +
  labs(title = "US employment: leisure and hospitality",
       y="Number of people (millions)")
	   
	   fit <- leisure %>%
  model(
  auto = ARIMA(Employed, stepwise = FALSE, approx = FALSE)
  )
  
  fc<-forecast(fit, h=36) 

 
  output$plot <- renderPlotly({
    plot<-fc %>% autoplot(leisure) +
  labs(title = "US employment: leisure and hospitality",
       y="Number of people (millions)")
    ggplotly(plot)
  })
}

shinyApp(ui, server)

Do you know if there is a way to include the interactive plot with forecasts in the Shiny app?
How is possible to show the forecasted values as table in the Shiny app?

Thank you in advance for your help.

One approach you could use to guarantee the correct formatting / display of the data is to format to columns as text:

  output$table <- renderTable({
    hilo(fc) %>% 
      # Revert to tibble as we will change the index to no longer be a time vector
      as_tibble() %>% 
      # Format custom classes into their text form for correct display in table.
      transmute(
        Month = format(Month),
        Forecast = .mean,
        "80%" = format(`80%`), "95%" = format(`95%`)
      )
  })

Other table packages such as {gt} may be better suited to correctly displaying these data types.

1 Like

Now the table displays with the correct formatting. Thank you so much @mitchelloharawild !

Do you know any approach to include the interactive plot with forecasts in the Shiny app?

Apologies for the late reply.

Making the forecast plots (with intervals) interactive is a bit hacky I'm afraid. The plot of forecast intervals is a custom geometry, which you would need to replicate in an interactive plotting library such as plotly. If you were only interested in plotting point forecasts, you could do this easily with plotly::ggplotly().

The {ggdist} package may make plotting multiple forecast intervals interactively easier, but I'm not sure of this.

1 Like

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.