How to solve 'argument x is empty' error in r (Shiny Web app)

Viewed 737

I am trying to learn to build a machine learning based shiny interface in R. I have spent hours in solving this 'argument 10 is empty' error and could not figure out how. My codes follow the sturcture created by @dataprofessor from Github. I will greatly appreciate any suggestions, hints and corrections. Thanks!

Here's my code:

# Import libraries
library(shiny)
library(data.table)
library(randomForest)
library(caret)
library(mlbench)
data("Glass")
# Read in the RF model
model <- readRDS("model.rds")

# Training set
set.seed(345)
train.index=createDataPartition(Glass[,ncol(Glass)],p=0.7,list=FALSE)
train=Glass[train.index,]


####################################
# User interface                   #
####################################

ui <- pageWithSidebar(
    
    # Page header
    headerPanel('Glass type Predictor'),
    
    # Input values
    sidebarPanel(
        HTML("<h3>Input parameters</h4>"),
        sliderInput("RI", label = "Refractive index", value = mean(train$RI),
                    min = min(train$RI),
                    max = max(train$RI)),
        sliderInput("Na", label = "Sodium", value = mean(train$Na),
                    min = min(train$Na),
                    max = max(train$Na)),
        sliderInput("Mg", label = "Magnesium", value = mean(train$Mg),
                    min = min(train$Mg),
                    max = max(train$Mg)),
        sliderInput("Al", label = "Aluminum", value = mean(train$Al),
                    min = min(train$Al),
                    max = max(train$Al)),
        sliderInput("Si", label = "Silicon", value = mean(train$Si),
                    min = min(train$Si),
                    max = max(train$Si)),
        sliderInput("K", label = "Potassium", value = mean(train$K),
                    min = min(train$K),
                    max = max(train$K)),
        sliderInput("Ca", label = "Calcium", value = mean(train$Ca),
                    min = min(train$Ca),
                    max = max(train$Ca)),
        sliderInput("Ba", label = "Barium", value = mean(train$Ba),
                    min = min(train$Ba),
                    max = max(train$Ba)),
        sliderInput("Fe", label = "Iron", value = mean(train$Fe),
                    min = min(train$Fe),
                    max = max(train$Fe)),
        
        actionButton("submitbutton", "Submit", class = "btn btn-primary")
    ),
    
    mainPanel(
        tags$label(h3('Status/Output')), # Status/Output Text Box
        verbatimTextOutput('contents'),
        tableOutput('tabledata') # Prediction results table
        
    )
)
####################################
# Server                           #
####################################

server<- function(input, output, session) {
    
    # Input Data
    datasetInput <- reactive({  
        
        df <- data.frame(
            Name = c("Refrative index",
                     "Sodium",
                     "Magnesium",
                     "Aluminum",
                     "Silicon",
                     "Potassium",
                     "Calsium",
                     "Barium",
                     "Iron"),
            Value = as.character(c(input$RI,
                                   input$Na,
                                   input$Mg,
                                   input$Al,
                                   input$Si,
                                   input$K,
                                   input$Ca,
                                   input$Ba,
                                   input$Fe,)),
            stringsAsFactors = FALSE)
        
        Type <- 5
        df <- rbind(df, Type)
        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()) 
        } 
    })
    
}

####################################
# Create the shiny app             #
####################################
shinyApp(ui = ui, server = server)

And here's the codes of model.RDS

library(randomForest)
library(mlbench)
data("Glass")
#################################################################
#################################################################
#########random split to training and test set##################
set.seed(345)
train.index=createDataPartition(Glass[,ncol(Glass)],p=0.7,list=FALSE)
train=Glass[train.index,]
test=Glass[-train.index,]
#################################################################
#########Random forest model#####################################
#################################################################
#################################################################
model <- randomForest(Type ~ ., data = train, ntree = 500, mtry = 9, importance = TRUE)
model
# Save model to RDS file
saveRDS(model, "model.rds")
1 Answers

The original problem was caused by a spurious comma after input$Fe. This then led to a second problem caused by the mismatch between the names of the test dataframe required by the code and the construction of this from the input values. I also needed add a call to library(caret) when building the model. It runs now and I can see that Type is being passed into the prediction call. Not sure why this is needed since the prediction is trying to come up with the Type, so I removed that. I also removed the creation of the input.csv file and just created the test data frame directly.

Here is the full app.R.

library(shiny)
library(data.table)
library(randomForest)
library(caret)
library(mlbench)
data("Glass")
# Read in the RF model
model <- readRDS("model.rds")

# Training set
set.seed(345)
train.index=createDataPartition(Glass[,ncol(Glass)],p=0.7,list=FALSE)
train=Glass[train.index,]

####################################
# User interface                   #
####################################

ui <- pageWithSidebar(
    
    # Page header
    headerPanel('Glass type Predictor'),
    
    # Input values
    sidebarPanel(
        HTML("<h3>Input parameters</h3>"),
        sliderInput("RI", label = "Refractive index", value = mean(train$RI),
                                min = min(train$RI),
                                max = max(train$RI)),
        sliderInput("Na", label = "Sodium", value = mean(train$Na),
                                min = min(train$Na),
                                max = max(train$Na)),
        sliderInput("Mg", label = "Magnesium", value = mean(train$Mg),
                                min = min(train$Mg),
                                max = max(train$Mg)),
        sliderInput("Al", label = "Aluminum", value = mean(train$Al),
                                min = min(train$Al),
                                max = max(train$Al)),
        sliderInput("Si", label = "Silicon", value = mean(train$Si),
                                min = min(train$Si),
                                max = max(train$Si)),
        sliderInput("K", label = "Potassium", value = mean(train$K),
                                min = min(train$K),
                                max = max(train$K)),
        sliderInput("Ca", label = "Calcium", value = mean(train$Ca),
                                min = min(train$Ca),
                                max = max(train$Ca)),
        sliderInput("Ba", label = "Barium", value = mean(train$Ba),
                                min = min(train$Ba),
                                max = max(train$Ba)),
        sliderInput("Fe", label = "Iron", value = mean(train$Fe),
                                min = min(train$Fe),
                                max = max(train$Fe)),
        
        actionButton("submitbutton", "Submit", class = "btn btn-primary")
    ),
    
    mainPanel(
        tags$label(h3('Status/Output')), # Status/Output Text Box
        verbatimTextOutput('contents'),
        tableOutput('tabledata') # Prediction results table
        
    )
)
####################################
# Server                           #
####################################

server<- function(input, output, session) {
    
    # Input Data
    datasetInput <- reactive({  
        
        df <- data.frame(
            Name = c("RI",
                     "Na",
                     "Mg",
                     "Al",
                     "Si",
                     "K",
                     "Ca",
                     "Ba",
                     "Fe"),
            Value = as.character(c(input$RI,
                                 input$Na,
                                 input$Mg,
                                 input$Al,
                                 input$Si,
                                 input$K,
                                 input$Ca,
                                 input$Ba,
                                 input$Fe)),
            stringsAsFactors = FALSE)

        input <- transpose(df)
        test = input[2,]
        names(test) = as.character(input[1,])
        
        Output <- data.frame(Prediction=predict(model,test), round(predict(model,test,type="prob"), 3))
        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()) 
        } 
    })
    
}

####################################
# Create the shiny app             #
####################################
shinyApp(ui = ui, server = server)
Related