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.