Aadil Nakhoda
2nd June 2020
str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
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)})
})