Aadil Nakhoda
2nd June 2020
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
library(shiny)
library(dplyr)
options(digits=2)
cars <- select(mtcars, cyl, hp, wt, disp, drat)
shinyUI(fluidPage(
titlePanel('Miles per Gallon and Car Features'),
sidebarLayout(
sidebarPanel(
selectInput('yin', 'Choose a feature (dependent variable)', names(cars),
selected=names(cars)[[1]]),
h1(""),
checkboxInput("showFit1", "Show Fitted Line", value = TRUE),
checkboxInput("showbeta1", "Show Slope Coefficient", value = TRUE),
checkboxInput("showConclusion", "Show Statement", value = TRUE),
h5("First 8 Observations"),
tableOutput('table')),
mainPanel(
h3("Question: Does enhancing features of cars reduce its miles per gallon?"),
h6("Note: Data extracted from 'mtcars' dataset"),
h3("Answer:"),
plotOutput("plot1"),
h4("Slope Coefficient and conclusion is:"),
textOutput("beta1"),
textOutput("beta2"),
textOutput("beta3"),
textOutput("beta4"),
textOutput("beta5"),
textOutput("text1"),
textOutput("text2"),
textOutput("text3"),
textOutput("text4"),
textOutput("text5")
)
)
))
<!–html_preserve–>
library(ggplot2)
library(shiny)
mtcars2<-mtcars
shinyServer(function(input, output) {
selectedData <- reactive({
mtcars[, c("mpg", input$yin)]})
lm1 <- lm(cyl ~ mpg, data = mtcars)
lm2 <- lm(wt ~ mpg, data = mtcars)
lm3 <- lm(hp ~ mpg, data = mtcars)
lm4 <- lm(disp ~ mpg, data = mtcars)
lm5 <- lm(drat ~ mpg, data = mtcars)
output$plot1 <- renderPlot({
par(mar = c(5.1, 4.1, 0, 1))
plot(selectedData(),
pch=20, cex = 3, xlab = "Miles per Gallon", ylab = input$yin)
if(input$showFit1){
if(input$yin=='cyl'){abline(lm1, col = "red", lwd = 2)}
if(input$yin=='wt'){abline(lm2, col = "green", lwd = 2)}
if(input$yin=='hp'){abline(lm3, col = "blue", lwd = 2)}
if(input$yin=='disp'){abline(lm4, col = "orange", lwd = 2)}
if(input$yin=='drat'){abline(lm5, col = "purple", lwd = 2)}
}
})
output$beta1 <- renderText({if(input$yin=='cyl' & input$showbeta1){lm1[[1]][2]}})
output$beta2 <- renderText({if(input$yin=='wt' & input$showbeta1 ){lm2[[1]][2]}})
output$beta3 <- renderText({if(input$yin=='hp' & input$showbeta1 ){lm3[[1]][2]}})
output$beta4 <- renderText({if(input$yin=='disp' & input$showbeta1 ){lm4[[1]][2]}})
output$beta5 <- renderText({if(input$yin=='drat' & input$showbeta1 ){lm5[[1]][2]}})
output$text1 <- renderText({if(input$yin=='cyl' & input$showConclusion){"Greater the number of cylinders lower the MPG"}})
output$text2 <- renderText({if(input$yin=='wt' & input$showConclusion){"Greater the weight lower the MPG"}})
output$text3 <- renderText({if(input$yin=='hp' & input$showConclusion){"Greater the horsepower lower the MPG"}})
output$text4 <- renderText({if(input$yin=='disp' & input$showConclusion){"Greater the displacement lower the MPG"}})
output$text5 <- renderText({if(input$yin=='drat' & input$showConclusion){"Larger the drat (real axle ratio) higher the MPG"}})
output$table <- renderTable({
head(selectedData(),8)})
})