Shiny Fuel Consumption App

Mercia Carolina Wentzel
September 24 2018

This presentation forms part of the final assignment of the Developing Data Products course offered by Johns Hopkins University at Coursera.org.

Part 1: Create a Shiny application and deploy it on Rstudio's servers.

I have used the R dataset named mtcars to illustrate:

  • a Shiny user interface that uses a slider to obtain car weight as input - see code on third slide.

  • a Shiny server that predicts fuel consumption based on the selected car weight - see code on fourth slide.

The Shiny Fuel Consumption app has duly been deployed here:

Shiny user interface code

library(shiny)

shinyUI(
  fluidPage(
    headerPanel(
      "Shiny fuel consumption app"
    ),
    sidebarPanel(
      sliderInput(
        inputId = 'selected_wt', 
        label = 'Slide to a car weight (1000 pounds) to predict fuel consumption (miles per gallon)',
        value = 1.513, min = 1.513, max = 5.424, step = 0.001
      ),
      submitButton('Submit')
    ),
    mainPanel(
      h3("Current car weight selection"), verbatimTextOutput("selected_wt"), h5("1000 pounds"), h3(""),
      h3("Predicted fuel consumption"), verbatimTextOutput("predicted_mpg"), h5("miles per gallon"), h3(""),
      plotOutput('plot')
    )
  )
)

Shiny server code

library(shiny)
library(datasets)

shinyServer(
  function(input, output) {

    output$selected_wt <- renderPrint({
      input$selected_wt
    })

    output$predicted_mpg <- renderPrint({
      predict_mpg(input$selected_wt)      
    })

    output$plot <- renderPlot({
      plot(
        mtcars$wt, mtcars$mpg, type="p", 
        main = "Car Weight and Fuel Consumption", 
        xlab = "Car Weight (1000 pounds)", 
        ylab = "Fuel Consumption (miles per gallon)") +
        abline(lm(formula = "mpg ~ wt", data = mtcars))
    })
  }
)

predict_mpg <- function(carweight_lbs) {
  fit <- lm(mpg ~ wt, data = mtcars)
  est <- predict(fit, data.frame(wt = carweight_lbs), interval = "predict")
  est[1,"fit"]
}

Part 2: Use Slidify or Rstudio Presenter to prepare a reproducible pitch presentation about your application.

This presentation was created using RStudio Presenter.

Thank you