Error: Object 'X' not found-- while deploying random forest model into shiny

I am trying to deploy random forest model into shiny. Here is the following information

# Import required libraries

library(shiny)
library(shinythemes)
library(data.table)
library(randomForest)
library(AER)
library(flexdashboard)

# Read the random forest built and saved from model.R

model <- readRDS("model.rds") ## This model was built earlier and saved and stored as model.rds in the same folder.

  1. ui.R code
######### User Interface #############
ui <- fluidPage(
  theme = shinytheme("flatly"),
  
  #Page header
  headerPanel('Prediction: Probability of Credit Card Approval'),
  
  #Input values
  sidebarPanel(
    HTML("<h5>Select Input Features</h5>"),
    
    selectInput("owner",
                label = "Owner",
                choices = list("yes" = "yes", "no" = "no"),
                selected = "yes"),
    selectInput("selfemp",
                label = "Selfemployed",
                choices = list("yes" = "yes", "no" = "no"),
                selected = "no"),
    
    #sliderInput("reports", 
    #            label = "Derogatory_Reports",
    #            min = 0,
    #            max = 14,
    #            value = 3),
    sliderInput("age",
                label = "Age",
                min = 1,
                max = 84,
                value = 33),
    sliderInput("income",
                label = "Income",
                min = 0.2,
                max = 14,
                value = 3.5),
    #sliderInput("share",
    #            label = "Share_of_CCard_Exp_to_Yrly_Inc",
    #            min = 0,
    #            max = 1,
    #            value = 0.10),
    #sliderInput("expenditure",
    #            label = "Avg_Mthly_CCard_Expenditure",
    #            min = 0,
    #            max = 3100,
    #            value = 185),
    #sliderInput("dependents",
    #            label = "No_of_Dependents",
    #            min = 0,
    #            max = 6,
    #            value = 2),
    #sliderInput("months",
    #            label = "Months_Living_at_Current_Address",
    #            min = 0,
    #            max = 540,
    #            value = 56),
    #sliderInput("majorcards",
    #            label = "No_of_Major_CCards_held",
    #            min = 0,
    #            max = 1,
    #            value = 0.8),
    #sliderInput("active",
    #            label = "No_of_Active_Credit_Accounts",
    #            min = 0,
    #            max = 46,
    #            value = 7),
    
    actionButton("submitbutton", "Submit", class = "btn btn-primary")
    #submitButton("Submit")
  ),
  
  mainPanel(
    tags$label(h3('Status of Credit Card Application')),
    verbatimTextOutput('contents'),
    tableOutput('tabledata')#, # Prediction results table
    # Output: Histogram ---
    #gaugeOutput('Scale')
  )
)

  1. server.R code
server <- function(input, output, session) {
  
  
  datasetInput <- reactiveValues(data = NULL)
  #Input data
  datasetInput <- reactive({
    
    df <- data.frame(
    Name = c("Owner",
             "Selfemployed",
             #"Derogatory_Reports",
             "Age",
             "Income"
             #"Share_of_CCard_Exp_to_Yrly_Inc",
             #"Avg_Mthly_CCard_Expenditure",
             #"No_of_Dependents",
             #"Months_Living_at_Current_Address",
             #"No_of_Major_CCards_held",
             #"No_of_Active_Credit_Accounts"
             ),
    Value = as.character(c(input$owner,
                           input$selfemp,
                           #input$reports,
                           input$age,
                           input$income
                           #input$share,
                           #input$expenditure,
                           #input$dependents,
                           #input$months,
                           #input$majorcards,
                           #input$active
                           )),
    stringsAsFactors = FALSE)
  
  card <- "card"
  
  
  df <- rbind(df, card)
  input <- transpose(df)
  write.table(input, "input.csv", sep = ",", quote = FALSE, row.names = FALSE, col.names = FALSE)
  #write.table(input, "input4.csv", sep = ",", quote = FALSE, row.names = FALSE, col.names = FALSE)
  
  test <- read.csv(paste("input", ".csv", sep=""), header = TRUE)
  
  #test$Owner <- factor(test$Owner, levels = c("yes", "no"))
  #test$Selfemployed <- factor(test$Selfemployed, levels = c("yes", "no"))
  #test$Age <- numeric(test$Age)
  #test$Income <- numeric(test$Income)
  
  Output <- data.frame(Prediction = predict(model,test), round(predict(model, test, type = "prob"), 3))
  
  print(Output)
  })
  
  #my_plot <- reactive({
  #  gauge(datasetInput()$Yes, min = 0, max = 100, symbol = '%', label = "Approval", gaugeSectors(
  #    success = c(80,100), warning = c(40, 79), danger = c(0, 39)
  #  ))
  #})
  
  #datasetInput()
  #output$Scale <- renderGauge({
  #  my_plot()
  #})

# Probability Text Box
output$contents <- renderPrint({
  if (input$submitbutton>0) {
    isolate("Decision Made.")
  } else {
    return("Click SUBMIT After Selecting the Values of Features")
  }
})

# Prediction results table
output$tabledata <- renderTable({
  if (input$submitbutton>0) {
    isolate(datasetInput())
  }
})
}
  1. error message.
Listening on http://127.0.0.1:6362
Warning: Error in eval: object 'age' not found
  133: eval
  132: eval
  131: model.frame.default
  129: predict.randomForest
  126: <reactive:datasetInput> [/Users/prithvirajlakkakula/Desktop/GitHubProjects/Shiny-App-For-Deploying-Random-Forest-Model/app.R#145]
  110: datasetInput
   97: renderTable [/Users/prithvirajlakkakula/Desktop/GitHubProjects/Shiny-App-For-Deploying-Random-Forest-Model/app.R#173]
   96: func
   83: renderFunc
   82: output$tabledata
    1: runApp

Any help is appreciated. Thank you.

R is case sensitive.
It seems when you built your model you did it on data with an 'age' variable.
and when you construct the data.frame below you call a column 'Age'.
This difference explains your error.

Thank you for your reply.
I do not think it was the case. Because, in the sliderInput of user interface, it was declared as age and then labeled as Age. Also, in the server.R code, the Value of the data.frame is also called with age. I could not find the solution and have been working on it for a week now.

Compare the last word of both quotes.
Good luck !

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