Udaya K Tejwani
March 2, 2021
Overview
This application displays a plot of mileage of different types of cars Vs their weight. It has Slider inputs to select:
minimum value on the x-axis
maximum value on the x-axis
minimum value on the y_axis
maximum value on the y-axis
The left control of the upper slider bar is used to choose the minimum value of the x-axis of the plot. The right control of the upper slider bar is used to choose the maximum value of the x-axis on the plot. The left control of the lower slider bar is used to choose the minimum value of the y-axis of the plot. The right control of the lower slider bar is used to choose the maximum value of the y-axis on the plot.
The application also fits a linear model to the data and displays it in the display, along with a mathematical description of the model.
This app is hosted at https://ubts.shinyapps.io/Course_9_Week_4_Project/ and at https://github.com/udayatejwani/Data-Science-Coursera/tree/master/Developing%20Data%20Products/Week%204%20Project
The application uses the following widgets:
sliderInput: The upper slider bar is used to select the minimum and maximum values for the x_axis.
The lower slider bar is used to select the minimum and maximum values for the y-axis.
Reactive output display area is for displaying the plot.
Application - ui.R
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("The Green Car"),
# Sidebar with a slider input for minimum and maximum values for x-axis and y-axis
sidebarLayout(
sidebarPanel(
sliderInput("sliderX", "Pick Minimum and Maximum X Values", 0, 6, value = c(0, 6)),
sliderInput("sliderY", "Pick Minimum and Maximum Y Values", 0, 25, value = c(0, 25))
),
# Show a plot
mainPanel(
("MPG Vs Weight of Car"),
plotOutput("plot1")
)
)
))
Application - server.R
library(shiny)
library(ggplot2)
data(mtcars)
# Define server logic required to draw a regression model representation
shinyServer(function(input, output) {
# Fit regression line mpg Vs. wt
require(stats)
reg <- lm(mpg ~ wt, data = mtcars)
coeff = coefficients(reg)
ltext <- paste("mpg = ", round(coeff[2],1), " * wt + ", round(coeff[1],1))
output$plot1 <- renderPlot({
minX <- input$sliderX[1]
maxX <- input$sliderX[2]
minY <- input$sliderY[1]
maxY <- input$sliderY[2]
gp <- ggplot(mtcars, aes(wt, mpg, color=cyl)) +
geom_point() +
xlim(minX, maxX) +
ylim(minY, maxY)
gp + geom_abline(intercept = coeff[1], slope = coeff[2], col="green", size = 1.5) + geom_text(x = minX + 2, y = (minY + maxY)/2, label = ltext, size = 10)
})
})