Final Project - Shiny App

Leandro Meili
Feb 5 - 2017

Introduction

This shiny app let you explore the Swiss Data set.

The app interface have

  • Sliders
  • Combo Boxes
  • Plot

How it Works

  • With the sliders, you choose the range of each variable

  • The combo boxes let you pick 2 variables

  • The scatter plot show the points selected with the sliders, and a line of the linear regression model between the 2 variables selected

Shiny - UI code

library(shiny)

shinyUI(fluidPage(
  titlePanel("Swiss Fertility and Socioeconomic Indicators (1888) Data"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("Fert","Fertility range", 0,100, value = c(0,100)),
      sliderInput("Agri","Agriculture range", 0,100, value = c(0,100)),
      sliderInput("Exam","Examination range", 0,100, value = c(0,100)),
      sliderInput("Educ","Education range", 0,100, value = c(0,100)),
      sliderInput("Cath","Catholic range", 0,100, value = c(0,100)),
      sliderInput("Mort","Mortality range", 0,100, value = c(0,100))

    ),
    mainPanel(
      selectInput("xAxis", "Select the X axis", 
                  colnames(swiss), 
                  selected = colnames(swiss)[1]),
      selectInput("yAxis", "Select the Y Axis", 
                  colnames(swiss), 
                  selected = colnames(swiss)[2]),
      h3("Scatter Plot between variables"),
      submitButton("Submit"),
      plotOutput("plot1")
    )
    )
  )
)

<!–html_preserve–>

Swiss Fertility and Socioeconomic Indicators (1888) Data

Scatter Plot between variables

<!–/html_preserve–>

Shiny - Server Code

library(shiny)
library(dplyr)

shinyServer(function(input, output) {
  output$plot1 <- renderPlot({
    base <- swiss
    base <- filter(base, base$Fertility > input$Fert[1] & base$Fertility < input$Fert[2])
    base <- filter(base, base$Agriculture > input$Agri[1] & base$Agriculture < input$Agri[2])
    base <- filter(base, base$Examination > input$Exam[1] & base$Examination < input$Exam[2])
    base <- filter(base, base$Education > input$Educ[1] & base$Education < input$Educ[2] )
    base <- filter(base, base$Catholic > input$Cath[1] & base$Catholic < input$Cath[2] )
    base <- filter(base, base$Infant.Mortality > input$Mort[1] & base$Infant.Mortality < input$Mort[2] )
    dataX <- base[,input$xAxis]
    dataY <- base[,input$yAxis]
    plot(dataX, dataY, 
         xlab = input$xAxis, 
         ylab = input$yAxis,
         xlim = c(0,100),
         ylim = c(0,100))
    model <- lm(dataY ~ dataX)
    abline(model, col = "blue", lwd=2)
  })
})