Shiny App to Predict Iris Species Given Petal Length & Width

Cat

09/03/2020

Background

In order to classify different species of Iris, we have created a tool which takes the Petal length and width as inputs and will return the predicted species.

The model is was trained on 70% of the data, and tested on the remaining 30%. The next tab displays a chart of the data split by species type.

set.seed(13579)
data <- datasets::iris
trainFlag <- createDataPartition(y = iris$Species, p=0.7, list = FALSE)
train <- data[trainFlag,]
qplot(train$Petal.Length, train$Petal.Width, colour = train$Species)

Train Dataset

Server Caclulation

The server code for the app is detailed below

options(shiny.sanitize.errors = FALSE)
require(caret)
require(e1071)

shinyServer(function(input,output) {
      output$modelPred <- reactive({
        set.seed(13579)
        data <- datasets::iris
        trainFlag <- createDataPartition(y = iris$Species, p=0.7, list = FALSE)
        train <- data[trainFlag,]
        test <- data[-trainFlag,]
        model <- train(Species ~ Petal.Width + Petal.Length,method = "rpart", data= train)
       
        widthNew <- input$sliderWidth
        lengthNew <- input$sliderLength
        newData <- data.frame(widthNew,lengthNew)
        names(newData) <- c("Petal.Width","Petal.Length")
        
        predict(model,newData)
      })
})