Iris Species prediction using a shiny app

José Oliver Segura
july, 29th, 2020

Summary

This presentation shows the work done to build a shiny app that predicts Iris Species based on use input that can be found at https://joseo.shinyapps.io/IrisPrediction/.

This application uses sliders as a form of input for the petal/sepal width/length predictors and outputs back a Species prediction based on these inputs. It also includes boxplots of each of the predictors based on species and overlays on them the users' input.

The applicatin also includes a help/usage tab in order to ease their use.

Application building

In order to build this application, the following steps have been performed:

  • Create a new shiny app using R's menus
  • Build a prediction model on the Iris dataset in R
  • Save that model to a file inside the Shiny App directory structure
  • Adapt the shiny app:
    • To load the model at startup
    • To present the users with a set of sliders to read their inputs
    • To read those inputs, make a prediction and show it to the user, outlining their inputs in the corresponding boxplots

Model building

library(dplyr)
library(caret)
data(iris)
inTrain <- createDataPartition(iris$Species, p=0.75, list=FALSE)
trainingData <- iris[inTrain,]
testingData <- iris[-inTrain,]
rfModel <- train(Species ~ ., data=iris,method="rf")
save(rfModel,file="Test/data/model.rda")
predicted <- predict(rfModel,testingData)
mean(predicted==testingData$Species)
[1] 1

Shiny application

We can highlight two important factors on the shiny app

  • Loading the model outside the shinyServer function call

    load(file="data/model.rda",.GlobalEnv)
    
  • Inside the shinyServer function call, using reactive programming to update our output

    predictionText<- reactive({
        newData <- data.frame(Sepal.Length=input$Sepal.Length,
        ...)
        p <- predict(rfModel,newData)
        levels(iris$Species)[p[1]]
        })