juillet 23, 2026

Introduction

This presentation introduces Iris Species Predictor, a Shiny app that classifies an iris flower into one of three species — setosa, versicolor, or virginica — based on four measurements entered by the user.

  • Built on top of R’s classic built-in iris dataset (150 flowers)
  • Uses a k-nearest-neighbours classifier (k = 5)
  • Fully interactive: move 4 sliders, get an instant prediction and plot

Live app: https://YOUR-USERNAME.shinyapps.io/iris_predictor/

The Data

The iris dataset ships with base R and contains 150 observations of 4 numeric measurements (in cm) for 3 species of iris flower.

str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

How the Prediction Works

The app takes the 4 values from the sliders and finds the 5 nearest neighbours in the training data (using Euclidean distance across all 4 measurements), then predicts the majority species among them.

library(class)
train  <- iris[, 1:4]
labels <- iris$Species

new_flower <- data.frame(Sepal.Length = 5.0, Sepal.Width = 3.4,
                          Petal.Length = 1.5, Petal.Width = 0.2)

knn(train = train, test = new_flower, cl = labels, k = 5)
## [1] setosa
## Levels: setosa versicolor virginica

The example above (small petals) is correctly classified as setosa.

Visualizing a Prediction

The app plots every flower in the dataset (colored by species) on a Petal Length vs. Petal Width chart, with the user’s input shown as a black star – making the reasoning behind the prediction visible.

speciesColors <- c(setosa = "#F8766D", versicolor = "#00BA38", virginica = "#619CFF")
plot(iris$Petal.Length, iris$Petal.Width,
     col = speciesColors[as.character(iris$Species)], pch = 19,
     xlab = "Petal Length (cm)", ylab = "Petal Width (cm)")
points(1.5, 0.2, pch = 8, cex = 2.5, lwd = 2)

Try It Yourself

  • App: https://YOUR-USERNAME.shinyapps.io/iris_predictor/
  • Source code (ui.R / server.R): https://github.com/YOUR-USERNAME/iris_predictor
  • Documentation on how to use the app is built directly into its “Documentation” tab — no external link needed
  • No statistics or R knowledge required: just move the sliders!

Thank you!