Shiny app for mtcars data

This is the supporting pitch for the created application using Shiny. The application is deployed on RStudio's Shiny server. Our Shiny app, named FinalProject aims to perform some exploratory analysis on the mtcars dataset.

  • We first created an interactive histogram plot of the miles per galon (mpg) data. Using the slider on the left, the user can change the number of bins. Using the checkbox on the left, the user can also choose whether the mean and the standard deviation range is visible on the histogram. We also created an interactive plot to show the relationship between the hoursepower (hp) and the miles per galon (MPG). The predicted hoursepower is also printed as text in the output.

Sample histogram created with the app

x    <- mtcars$mpg  
bins <- seq(min(x), max(x), length.out = 12)
hist(x, breaks = bins, col = 'green', border = 'black', xlab = "MPG", cex.lab = 2.5, cex.axis = 2.5, cex.main = 5)
abline(v = mean(x), col = "red", lwd = 2)
abline(v = c(mean(x) - sd(x), mean(x) + sd(x)), col = "orange", lwd = 3, lty = 2)

plot of chunk unnamed-chunk-1

Sample slides created with the app

model_lm <- lm(hp~mpg, data = mtcars)
plot(mtcars$mpg, mtcars$hp, xlab = "MPG", ylab = "Hoursepower", cex.lab = 2.5, cex.axis = 2.5, cex.main = 5, cex = 4, pch = 17)
abline(model_lm, col = "red", lwd = 2)
model_lm_pred <- predict(model_lm, newdata = data.frame(mpg = 20))
points(20, model_lm_pred, col = "blue", pch = 16, cex = 2)

plot of chunk unnamed-chunk-2

ui.R code

library(shiny)
shinyUI(fluidPage(
  titlePanel("mtcars dataset"),
  sidebarLayout(
    sidebarPanel(
       sliderInput("bins",
                   "Number of bins:",
                   min = 1,
                   max = 35,
                   value = 12),
       checkboxInput("showmeanstd", 'Show/hide the mean value', value = TRUE),
       sliderInput("sliderMPG",
                   "MPG of a car?",
                   min = 10,
                   max = 35,
                   value = 20),
       checkboxInput("showmodel", 'Show/hide the predicted model', value = TRUE)
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Plot",
                 fluidRow(
                   column(8, plotOutput("histPlot")),
                   column(8,  h3("Predicted hoursepower from the model:"), textOutput("pred")),
                   column(8, plotOutput("lmplot"))
                 ))
      ))
  )
))

<!–html_preserve–>

mtcars dataset

Predicted hoursepower from the model:

<!–/html_preserve–>

server.R code

library(shiny)
shinyServer(function(input, output) {
  output$histPlot <- renderPlot({
    x    <- mtcars$mpg  
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins, col = 'green', border = 'black', xlab = "MPG")
    if (input$showmeanstd){
      abline(v = mean(x), col = "red", lwd = 2)
      abline(v = c(mean(x) - sd(x), mean(x) + sd(x)), col = "orange", lwd = 3, lty = 2)
    }
  })
    model_lm <- lm(hp~mpg, data = mtcars)
    model_lm_pred <- reactive({
      mpgInput <- input$sliderMPG
      predict(model_lm, newdata = data.frame(mpg = mpgInput))
    })
    output$lmplot <- renderPlot({
      mpgInput <- input$sliderMPG
      plot(mtcars$mpg, mtcars$hp, xlab = "MPG", ylab = "Hoursepower")
      if (input$showmodel){
        abline(model_lm, col = "red", lwd = 2)}
      points(mpgInput, model_lm_pred(), col = "blue", pch = 16, cex = 2)
  })
    output$pred <- renderText({
      model_lm_pred()})
})