August 07, 2023

Description of the mtcars Dataset

  • mpg: Miles per gallon (fuel efficiency).
  • cyl: Number of cylinders in the engine.
  • disp: Displacement (cubic inches) of the engine.
  • hp: Horsepower of the car.
  • drat: Rear axle ratio.
  • wt: Weight of the car (in thousands of pounds).
  • qsec: 1/4 mile time (seconds) for 0 to 60 mph acceleration.
  • vs: Engine type (0 = V-shaped, 1 = straight).
  • am: Transmission type (0 = automatic, 1 = manual).
  • gear: Number of forward gears.
  • carb: Number of carburetors.

About The App

  • Title: Car MPG Predictor App
  • Purpose: Predict MPG for selected car model based on weight

Instructions:

  1. Select a car model.
  2. Enter the weight of the car.
  3. Get a prediction for the MPG.

Links

Sample Code

# Define UI
ui <- fluidPage(
        # Define server logic
        shinyServer(function(input, output) {
                
                # Reactive function to calculate predicted MPG
                predicted_mpg <- reactive({
                        car_data <- subset(mtcars, carb == input$car_model)
                        predict_mpg <- car_data$mpg - (car_data$wt - input$weight) * 0.2
                        return(predict_mpg)
                })
                
                # Display predicted MPG as output
                output$mpg_output <- renderPrint({
                        paste("Predicted MPG:", predicted_mpg())
                })
        })
)

# Define server
server <- function(input, output) {
        
        # Reactive function to calculate predicted MPG
        predicted_mpg <- reactive({
                car_data <- subset(mtcars, carb == input$car_model)
                predict_mpg <- car_data$mpg - (car_data$wt - input$weight) * 0.2
                return(predict_mpg)
        })
        
        # Display predicted MPG as output
        output$mpg_output <- renderPrint({
                paste("Predicted MPG:", predicted_mpg())
        })
}

# Create the Shiny app
shinyApp(ui = ui, server = server)