Hello,
I 've a strange problem. The code below, was first working but than i got the error "cannot find the files", meaning on the rds files to load. The RDS files are all in the same map as the app.r file.
library(shiny)
library(e1071)
library(readr)
# Define a simple model (replace with your actual model)
# Example: salesNewspaper ~ Sales
#model1 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/svr_modelNewspaper.rds')
model2 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/modelTVlin.rds')
model3 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/svr_modelRadio.rds')
model4 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/saleslin.rds')
model1 <-readRDS('./svr_modelNewspaper.rds')
ui <- fluidPage(
titlePanel("Sales Prediction App"),
sidebarLayout(
sidebarPanel(
textInput("sales_value", "Enter Totalinvestment Value between 0 and 27 :", value = "100"),
actionButton("calculate", "Calculate")
),
mainPanel(
h4("Predicted salesNewspaper:"),
verbatimTextOutput("resultNews"),
h4("Predicted investment Radio:"),
verbatimTextOutput("resultradio"),
h4("Predicted investment TV:"),
verbatimTextOutput("resultTV"),
h4("Predicted sales:"),
verbatimTextOutput("resultSales")
)
)
)
server <- function(input, output) {
output$result <- eventReactive(input$calculate,
{
sales_value <-as.numeric(input$sales_value)
# Predict using the model, if value Sales is 0 or prediction less than 0, then result should be zero
# Predict Sales based on Investment, and use it as basic of calculation per media
#investment Newspaper
new_data <- data.frame(Total_invest = sales_value)
predictionNews <- predict(model1, newdata = new_data)*sales_value/100
output$resultNews <- renderText({ predictionNews })
#investment Radio
new_data <- data.frame(Total_invest = sales_value)
predictionR <- predict(model3, newdata = new_data)*sales_value/100
output$resultradio <- renderText({ predictionR })
#investment TV
new_data <- data.frame(Total_invest = sales_value)
predictionT <- predict(model2, newdata = new_data)
output$resultTV <- renderText({ predictionT })
#Sales
new_data <- data.frame(Total_invest = sales_value)
predictionS <- predict(model4, newdata = new_data)
output$resultSales <- renderText({ predictionS })
})
}
shinyApp(ui, server)
What do you think is the problem?
Kind regards,
Nobel
Is the problem that it cannot find any of the .rds files, or just one or more specific files? You can try commenting out all but one of the file readRDS() calls at a time to find out. Also, it might help to see the exact wording of the error message.
Hi guys and ladies,
I found the problem, but i do not understand why it is working.
So my solution is that the first output $result in the server function may not be changed in something else otherwise all is blocking. For all other outputs i can give it names like i did. They got updated , has someone an explanation?
Kind regards,
Nobel
ps why is creating shiny apps so complicated, are there beter solutions?
You are reading the data in at the top of your script. This part of the code only gets run once and while it works on your computer, this causes issues if this app was ever to be deployed to a server. -> Move the data loading inside the server function.
You are using eventReactive to return something to output$result, but the function does not return anything, it just updates several output slots. And there is no UI slot with ID of 'result' either, anyway. -> Use observeEvent() instead.
library(shiny)
library(e1071)
library(readr)
# Define a simple model (replace with your actual model)
# Example: salesNewspaper ~ Sales
ui <- fluidPage(
titlePanel("Sales Prediction App"),
sidebarLayout(
sidebarPanel(
textInput("sales_value", "Enter Totalinvestment Value between 0 and 27 :", value = "100"),
actionButton("calculate", "Calculate")
),
mainPanel(
h4("Predicted salesNewspaper:"),
verbatimTextOutput("resultNews"),
h4("Predicted investment Radio:"),
verbatimTextOutput("resultradio"),
h4("Predicted investment TV:"),
verbatimTextOutput("resultTV"),
h4("Predicted sales:"),
verbatimTextOutput("resultSales")
)
)
)
server <- function(input, output) {
# Read in data files whenever a Shiny session starts
#model1 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/svr_modelNewspaper.rds')
model2 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/modelTVlin.rds')
model3 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/svr_modelRadio.rds')
model4 <-readRDS('G:/projectsdatamining/portfolios/SalesPrediction/goodapp/saleslin.rds')
model1 <-readRDS('./svr_modelNewspaper.rds')
# Listen for the Calculate button to be clicked, perform calculations, and update all the outputs
observeEvent(input$calculate, {
sales_value <-as.numeric(input$sales_value)
# Predict using the model, if value Sales is 0 or prediction less than 0, then result should be zero
# Predict Sales based on Investment, and use it as basic of calculation per media
#investment Newspaper
new_data <- data.frame(Total_invest = sales_value)
predictionNews <- predict(model1, newdata = new_data)*sales_value/100
output$resultNews <- renderText({ predictionNews })
#investment Radio
new_data <- data.frame(Total_invest = sales_value)
predictionR <- predict(model3, newdata = new_data)*sales_value/100
output$resultradio <- renderText({ predictionR })
#investment TV
new_data <- data.frame(Total_invest = sales_value)
predictionT <- predict(model2, newdata = new_data)
output$resultTV <- renderText({ predictionT })
#Sales
new_data <- data.frame(Total_invest = sales_value)
predictionS <- predict(model4, newdata = new_data)
output$resultSales <- renderText({ predictionS })
})
}
shinyApp(ui, server)