Introduction

This is the final assignment for the Coursera course “Developing Data Products”. The aim of this course project is to build and demonstrate my knowledge of Shiny by building an app. I have created an app that shows four histograms, each illustrating the mean width and length of the petals and sepals of three species of iris.

Background

This application is written in Shiny, a web application framework for R. The source code consists of two files: server.R and ui.R, whereby the former includes the backend of a Shiny web application- and the latter the user-interface elements.

You can find the the Shiny application at https://….

The Shiny app source code is available at https://github.com/……

The Iris Dataset

I used R’s in-built “Iris” data set which contains various measurments for 50 flowers belonging to the species “iris setosa”, “iris versicolor” and “iris virginica”.

# looking at the dataset and getting the column names
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

ui.R

# loading the required packages 
library(shiny) # for the shiny App
library(ggplot2) # for plotting
library(gridExtra) # for arranging the plots generated by ggplot2 neatly

# code for ui.R: controls how the App looks 
shinyUI(pageWithSidebar(
  headerPanel('Visualising the Differences in Length and Width of the Petal and Sepal of Three Iris Species'),
  sidebarPanel(
    selectInput('Species', 'Select Species', as.character(unique(iris$Species)))
  ),
  mainPanel(
    plotOutput('irisplot',width = 400, height = 600)
  )
))

server.R

# code for server.R: controls what the app does
shinyServer(
  function(input, output) {
    data <- reactive({iris[iris$Species == input$Species,]})
    output$irisplot <- renderPlot({
    # Make four plots for the four parameters
      # Plot 1: Sepal Length 
      plot_SL <- ggplot(data(), aes(Sepal.Length)) +
        geom_histogram(bins=20, fill="blue", color = "black",alpha = .2) +
        geom_vline( aes(xintercept = mean(data()$Sepal.Length)), colour="yellow", size=1, alpha=1) +
        labs(x = "Sepal Length", y = "Frequency", title = paste("Sepal Length, mean =", round(mean(data()$Sepal.Length),2),"cm")) +
        theme(panel.background = element_rect(fill='transparent'), plot.background = element_rect(fill='transparent', color=NA))
      
      # Plot 2: Sepal Width
      plot_SW <- ggplot(data(), aes(Sepal.Width)) +
        geom_histogram(bins=20, fill="blue", color = "black",alpha = .2) +
        geom_vline( aes(xintercept = mean(data()$Sepal.Width)), colour="yellow", size=1, alpha=1) +
        labs(x = "Sepal Width", y = "Frequency", title = paste("Sepal Width, mean =", round(mean(data()$Sepal.Width),2),"cm"))+
        theme(panel.background = element_rect(fill='transparent'), plot.background = element_rect(fill='transparent', color=NA))
      
      # Plot 3: Petal Length
      plot_PL <- ggplot(data(), aes(Petal.Length)) +
        geom_histogram(bins=20, fill="blue", color = "black",alpha = .2) +
        geom_vline( aes(xintercept = mean(data()$Petal.Length)), colour="yellow", size=1, alpha=1) +
        labs(x = "Petal Length", y = "Frequency", title = paste("Petal Length, mean =", round(mean(data()$Petal.Length),2),"cm"))+
        theme(panel.background = element_rect(fill='transparent'), plot.background = element_rect(fill='transparent', color=NA))
      
      # Plot 4: Petal Width
      plot_PW <- ggplot(data(), aes(Petal.Width)) +
        geom_histogram(bins=20, fill="blue", color = "black",alpha = .2) +
        geom_vline( aes(xintercept = mean(data()$Petal.Width)), colour="yellow", size=1, alpha=1) +
        labs(x = "Petal Width", y = "Frequency", title = paste("Petal Width, mean =", round(mean(data()$Petal.Width),2),"cm"))+
        theme(panel.background = element_rect(fill='transparent'), plot.background = element_rect(fill='transparent', color=NA))
      
      # Plotting 4 graphs together
      grid.arrange(plot_SL,plot_SW, plot_PL, plot_PW,nrow=4, ncol=1)
    })
  }
)