Shinyapp Instructions

Himank Jain
01/09/2019

Introduction

The Shiny App can be found here knitr::include_graphics(“https://himankjn.shinyapps.io/ShinyApp/”)

The app provides a Graphical User Interface to perform exploratory Data analysis and linear regression on mtcars Dataset. The app contains three tabls all together.

The First Tab is specifically meant for exploratory data analysis using visualization. The visualization code was mostly written though R ggplot2 library.

The Second Tab is meant for performing linear regression on mtcars dataset with mpg as dependent variable disp as independent variable.

The Third Tab can be used to perform the same linear regression but only on the selected observations. The observations can be selected using brush(drag and select).

Dataset Fields

The following variables are available in mtcars dataset:

  • mpg Miles/(US) gallon
  • cyl Number of cylinders
  • disp Displacement (cu.in.)
  • hp Gross horsepower
  • drat Rear axle ratio
  • wt Weight (lb/1000)
  • qsec ¼ mile time
  • vs V/S
  • am Transmission Type
  • gear Number of forward gears
  • carb Number of carburetors

ui.r code

library(shiny)
library(ggplot2)
dataset<-mtcars
shinyUI(fluidPage(

    titlePanel("Project Shiny!"),
    mainPanel(
        h4("This App contains 3 tabs:"),
        HTML("<b> Exploratory Analysis</b> tab can be used to perform exploratory analysis on mtcars dataset <br><b> 
           Linear Regression</b> tab can be used for performing linear regression on mtcars dataset <br> <b> Linear Regression on sample</b> tab can be used for performing linear
           regression on a sample of mtcars dataset. The sample can be selected using a brush."),


    tabsetPanel(
        type="tabs",
        tabPanel("Exploratory Analysis
                 ",
                 br(),

                 h1("Exploratory Analysis on MtCars"),
                 sidebarLayout(
                     sidebarPanel(
                         sliderInput("n","Number of Observations?",10,nrow(dataset),value=10,step=5),
                         selectInput("xvar","X",names(dataset)),
                         selectInput("yvar","Y",names(dataset)),
                         selectInput("colorvar","Color",names(dataset)),
                         checkboxInput("smooth","smoothing curve?",value=FALSE)
                     ),
                     mainPanel(
                         h2("PLOT"),
                         plotOutput("xyplot")
                     )
                 )


        ),
        tabPanel("Linear Regression",
                 br(),

                 h1("Linear Regression on MTcars"),
                 sidebarLayout(
                     sidebarPanel(
                         textInput("box1","Tab2 Heading?",value="Linear Regression"),
                         sliderInput("slider1","Select mpg to predict disp!",5,35,value=20),
                         h3("Intercept:"),
                         textOutput("intout"),
                         h3("Slope:"),
                         textOutput("slopeout")


                     ),
                     mainPanel(
                         code("plot(mtcars$mpg,mtcars$disp,pch=16,col='blue',cex=2)"),
                         plotOutput("plot1"),
                         h2("predicted value:"),
                         h3( textOutput("pred"))


                     )

        )

        ),
        tabPanel("Linear Regression on Sample",
                 br(),

                 h1("Multiple Models"),
                 sidebarLayout(
                     sidebarPanel(
                         h2("Drag and Select a number of points to fit a linear model to them !"),
                         h3("Slope"),
                         textOutput("slopeOut"),
                         h3("intercept"),
                         textOutput("intOut")
                     ),
                     mainPanel(
                         h2("Data"),
                         plotOutput("plot2",brush=brushOpts(
                             id="brush1"
                         ))
                     )
                 )

                 )
        ))
))

<!–html_preserve–>

Project Shiny!

This App contains 3 tabs:

Exploratory Analysis tab can be used to perform exploratory analysis on mtcars dataset
Linear Regression tab can be used for performing linear regression on mtcars dataset
Linear Regression on sample tab can be used for performing linear regression on a sample of mtcars dataset. The sample can be selected using a brush.

Exploratory Analysis on MtCars

PLOT


Linear Regression on MTcars

Intercept:

Slope:

plot(mtcars$mpg,mtcars$disp,pch=16,col='blue',cex=2)

predicted value:


Multiple Models

Drag and Select a number of points to fit a linear model to them !

Slope

intercept

Data

<!–/html_preserve–>

server.r code

library(shiny)

shinyServer(function(input, output) {

    dataset<-reactive({
        mtcars[sample(nrow(mtcars),input$n),]
    })
    output$xyplot<-renderPlot({
        g<-ggplot(data=dataset(),aes_string(x=input$xvar,y=input$yvar,col=input$colorvar))+geom_point()+labs(x=input$xvar,y=input$yvar)
        if(input$smooth){
        g<-g+geom_smooth()
        }
        g
    })


    model<-reactive({
        lm(data=mtcars,disp~mpg)
    })
    output$intout<-renderText({
        model()[[1]][1]
    })
    output$slopeout<-renderText({
        model()[[1]][1]
    })

    prediction<- reactive({
        predict(model(),newdata=data.frame(mpg=input$slider1))
    })
    output$plot1<-renderPlot({
        plot(mtcars$mpg,mtcars$disp,pch=16,col='blue',cex=2)
        abline(model(),col='red',lwd=2)
        points(input$slider1,prediction(),pch=16,cex=3)
    })
    output$pred<-renderText({prediction()})




    model2<-reactive({
        brushed_data<-brushedPoints(mtcars,input$brush1,
                                    xvar='mpg',yvar='disp')
        if(nrow(brushed_data)<2){
            return(NULL)
        }
        lm(disp~mpg,data=brushed_data)
    })
    output$slopeOut<-renderText({
        if(is.null(model2())){
            "No model found"
        }
        else{
            model2()[[1]][2]
        }
    })
    output$intOut<-renderText({
        if(is.null(model2())){
            "No model found"
        }
        else{
            model2()[[1]][1]
        }
    })
    output$plot2<-renderPlot({
        plot(mtcars$mpg,mtcars$disp,pch=16,cex=2,xlab="MPG",ylab="DISP")
        if(!is.null(model2())){
            abline(model2(),col='blue',lwd=2)
        }
    })
})

Sample Image from the Application:

This image is a sample image from the actual application.

The link for the application can be found on next page.

Link To Mtcars ShinyApp

SHINY APP

THANK YOU!