08/11/2020

Introduction

The shiny app can be found here

This application provides a Graphical user interface to perform linear regression on mpg vs user selected independent variables and a plotter to select variables for understanding the relationship. This app contains 2 tabs.

The First Tab is meant for performing linear regression on mtcars dataset with mpg as dependent variable and variables selected from App as independent variables.

The Second Tab is for plotting graphs to visualize the relationship between mpg (dependent variable) and independent variables.

Dataset

mtcars dataset has 11 variables and all are numeric.

  • mpg Miles/(US) gallon
  • cyl Number of cylinders
  • disp Displacement (cu.in.)
  • hp Gross horsepower
  • drat Rear axle ratio
  • wt Weight (1000 lbs)
  • qsec 1/4 mile time
  • vs Engine (0 = V-shaped, 1 = straight)
  • am Transmission (0 = automatic, 1 = manual)
  • gear Number of forward gears
  • carb Number of carburetors

R code - UI

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
    
    # Application title
    titlePanel("Shiny Project"),
    mainPanel(
        h2("This App has 2 tabs :"),
        HTML("<b> Linear Regression </b> Tab is used to perform linear regression based on selecting single or multiple independent variables <br>
             <b> Plotter </b> Tab is used for visulaization of the independent variables against mpg of mtcars dataset"),
        tabsetPanel(
            type ="tabs",
            tabPanel("Linear Regression
                 ", 
                     br(),
                     h1("Linear Regression analysis on mtcars"),
                     br(),
                     sidebarLayout(
                         sidebarPanel(
                             selectInput("cols","Independent Variables to Select",
                                         choices=names(mtcars[,-1]),selected = names(mtcars[,-1])[[8]],
                                         multiple=TRUE)
                         ),
                         mainPanel(
                             verbatimTextOutput(outputId = "RegOut"),
                             plotOutput("RegPlot")
                         )
                     )
            ),
            tabPanel("Plotter",
                     br(),
                     h1("Plotter on mtcars"),
                     br(),
                     sidebarLayout(
                         sidebarPanel (
                             selectInput("x","X-Axis",choices=names(mtcars[-1]),multiple=FALSE),
                             selectInput("y","Y-Axis",choices=names(mtcars[1]),multiple=FALSE),
                             selectInput("color","Color",c("None",names(mtcars[-1]))),
                             selectInput("size","Size",c("None",names(mtcars[-1]))),
                             checkboxInput("smooth","Smooth"),
                             selectInput("fac_row","Facet Row",c(None=".",names(mtcars[-1]))),
                             selectInput("fac_col","Facet Column",c(None=".",names(mtcars[-1])))
                         ),
                         mainPanel(
                             plotOutput("plotOut")
                         )
                     )
                     
            )
            
            
        )
    )
))

R Code Server

library(shiny)
library(recipes)
library(ggplot2)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
    
  lm_formula <- reactive({
      mtcars %>%
          recipe() %>%
          update_role(mpg, new_role ="outcome") %>%
          update_role(!!!input$cols, new_role = "predictor") %>%
          formula()
  })
  
  lm_reg <- reactive({
      lm(lm_formula(), data = mtcars)
  })
  
  output$RegOut  <- renderPrint({
      summary(lm_reg())
  })
   
  output$RegPlot <- renderPlot({
    par(mfrow = c(2,2))
    plot(lm_reg())
  })
   
  output$plotOut <- renderPlot({
    p <- ggplot(data=mtcars, aes_string(x= input$x, y= input$y)) + geom_point()
    p <- p + labs(x =input$x, y = input$y)
    if(input$color != "None") {
      p <- p + aes_string(color = input$color)
    }
    
    if (input$size != "None") {
      p <- p + aes_string(size = input$size)
    }
    
    if(input$smooth){
      p <- p + geom_smooth()
    }
    
    facets <- paste(input$fac_row,"~",input$fac_col)
    if (facets != ".~."){
      p <- p + facet_grid(facets)
    }
    p
  }) 
  
})