R Shiny app error Error in eval: object 'age' not found

496 Views Asked by At

I am trying to create an app using shiny which uses a classification model (svm) to predict the output. But i keep getting this error: Error in eval: object 'age' not found

UI and SERVER code:

library(shiny)
library(data.table)
library(caret)
library(caretEnsemble)
library(shinythemes)

model <- readRDS("model.rds")
    

ui <- pageWithSidebar(
  headerPanel('Heart disease predictor'),
  sidebarPanel(
    tags$label(h3('Input parameters')),
    numericInput("age", label = "Age", value = 40),
    numericInput("sex", label = "Sex", value = 1),
    numericInput("chest.pain", label = "Chest pain", value = 1),
    numericInput("resting.BP", label = "resting Bp", value = 120),
    numericInput('cholesterol', label = 'Cholesterol', value = 170),
    numericInput('fasting.sugar', label = 'Fasting Sugar', value = 1),
    numericInput('ECG.at.rest', label = 'ECG at rest', value=1),
    numericInput('max.hear.rate', label = 'Max heart rate', value = 120),
    numericInput('exercisal.angina', label = 'Excercise induced angina', value = 0),
    numericInput('ST.segments', label = 'ST segments', value = 1.5),
    numericInput('slope.of.ST', label = 'slope of ST segment', value = 1),
    numericInput('number.of.vessels', label = 'number of coronary artery', value = 3),
    numericInput('thalassemia', label = 'thalassemia', value = 2),
    
    actionButton("submitbutton", "Submit", class = "btn btn-primary")
  ),
  mainPanel(
    tags$label(h3('Status/Output')), 
    verbatimTextOutput('contents'),
    tableOutput('tabledata')
    )
)


server<- function(input, output) {
  datasetInput <- reactive({  
    df <- data.frame(
      Name = c("Age",
               "Sex",
               "Chest pain",
               "resting Bp",
               "Cholesterol",
               "Fasting Sugar",
               "ECG at rest",
               "Max heart rate",
               "Excercise induced angina",
               "ST segments",
               "slope of ST segment",
               "number of coronary artery",
               "thalassemia"),
      Value = as.character(c(input$age,
                             input$sex,
                             input$chest.pain,
                             input$resting.BP,
                             input$cholesterol,
                             input$fasting.sugar,
                             input$ECG.at.rest,
                             input$max.hear.rate,
                             input$exercisal.angina,
                             input$ST.segments,
                             input$slope.of.ST,
                             input$number.of.vessels,
                             input$thalassemia)),
      stringsAsFactors = FALSE)
    
    target<- 0
    df <- rbind(df, target)
    input <- transpose(df)
    write.table(input,"input.csv", sep=",", quote = FALSE, row.names = FALSE, col.names = FALSE)
    
    test <- read.csv(paste("input", ".csv", sep=""), header = TRUE)
    
    Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3))
    print(Output)
    
  })
  
  # Status/Output Text Box
  output$contents <- renderPrint({
    if (input$submitbutton>0) { 
      isolate("Calculation complete.") 
    } else {
      return("Server is ready for calculation.")
    }
  })
  
  # Prediction results table
  output$tabledata <- renderTable({
    if (input$submitbutton>0) { 
      isolate(datasetInput()) 
    } 
  })
  
}

shinyApp(ui = ui, server = server)

I am new in this field and this is my first app which I am creating. Tried going through shiny tutorial but still i couldn't resolve this error. What did I do wrong?

2

There are 2 best solutions below

0
On

After going through a few changes i solved this error:

server<- function(input, output) {
  datasetInput <- reactive({  
    df <- data.frame(
      Name = c("Age",
               "Sex",
               "Chest pain",
               "resting Bp",
               "Cholesterol",
               "Fasting Sugar",
               "ECG at rest",
               "Max heart rate",
               "Excercise induced angina",
               "ST segments",
               "slope of ST segment",
               "number of coronary artery",
               "thalassemia"),

In here the names should match exactly with the columns of trained data, i did that it executed successfully.

1
On

When I replaced Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3)) with Output <- data.frame(test), it worked fine. So, the error occurs at predict(model,test).

Without the model.rds object provided, this is all I can look at.