Shiny app no results

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
1 Like

Are you running this locally (on your PC/laptop) or hosted on a server?

1 Like

I run it localy on a windows 11 PC. the folder is on a flash memory card.
Thx for your question, i forgot to write this

1 Like

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.

1 Like

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?

1 Like

What I see could be problematic is:

  1. 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.

  2. 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)
1 Like

He folks,
thx for reply, I reconstructed all myself and now is it working fine.
Kind regards,
Nobel

1 Like

This topic was automatically closed 7 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.