I have a shiny app that works fine when run locally. The app predicts a value using the user inputs and a linear model. The linear model was estimated beforehand- i.e., the app does not estimate the linear model it only predicts.
Here a reproducible example
#load data
data(mtcars)
#Estimate linear model
lm_t<-lm(hp~as.factor(cyl),data = mtcars)
#Save lm object as .rda
save(lm_t, file="mod.rda")
Use saved model in shiny app
library(shiny)
# Global
# Load preestimated linear model
load("mod.rda")
# Define UI ----
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# User input - number of months
selectInput("cyl",h4("Número de meses desde el lanzamiento del proyecto"), choices = c(4,6,8))
),
mainPanel(
#Display user input value
tableOutput("TBL1"),
#Display user prediction
textOutput("pred")
)
)
)
# Define server logic ----
server <- function(input, output) {
# Data frame for storing input values
dfs<-reactive({
dfp<-data.frame("Periodo" = input$cyl)
})
# Display input values
output$TBL1 <- renderTable({dfs()})
# Predict using linear model
pr<-reactive({
round(predict(lm_t,newdata = dfs()),3)
})
# Display prediction
output$pred<-renderText({paste("Se espera un incremento en el precio de:",pr(),"%")})
}
# Run the application
shinyApp(ui = ui, server = server)
The problem is that once deployed in shiny.io the app doesn't work. Error message: “The application failed to start. exit status 1”.
Looking into the logs it appears the problem is that the shiny can't find the mod.rda. I am 100% sure that I uploaded the mod.rda file when I deployed the app.
Any help in making the app work will be much appreciated.