How to visualize the data over Web using Shiny + R

Open Data Camp 2013, Hyderabad

Neependra Khare

R

  1. R is a free software environment for statistical computing and graphics
  2. Excellent for data visualization
  3. Very active community
  4. ..but..
    • Its a very personal experience
    • Sharing is not easy

Shiny

Shiny = R + interactivity + web + easy

Shiny

  1. Turns R analyses into interactive web applications
  2. No HTML or JavaScript knowledge is necessary
  3. But fully customizable and extensible with HTML/CSS/JavaScript
  4. Web UI
  5. Uses “reactive” programming model
  6. Free and Open Source (GPL-3)

Making an App

  1. Create a directory for the app
  2. Create two file in that directory
    • ui.R – defines user interface
    • server.R – specifies the logic behind user interface

ui.R

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel('District Wise Rural Health Care Infrastructure'),
  sidebarPanel(
    selectInput("dataset", "Choose a States/Union Territory:",
                choices = states),
    selectInput("center", "Choose a center:",
                choices = centers ),

    downloadButton('downloadData', 'Download')
  ),
  mainPanel(
    plotOutput('plot'),
    tableOutput('table')
  )
))

server.R

shinyServer(function(input, output) {
  datasetInput <- reactive({
        state = input$dataset
        center = input$center
        df = read.csv("District_Wise_Rural_HealthCare_Infrastructure.csv")
        sub = subset.data.frame(df, df$States.Union.Territory == state)
        ndf = data.frame(District = sub$Name.of.the.District, Centers = eval(parse(text = paste0("sub$",center))))
  })

  output$table <- renderTable({
    datasetInput()
  })
})

Demo 1

  • District-Wise Availability Of Health Centres In India as of March 2011

  • Data Source

  • Running from self-hosted Shiny Server

    • http://localhost:3838/demo1/
  • Running from RStudio-hosted Shiny Server

    • install.packages('shiny')
    • shiny::runGist('5835958')

Demo 2

Deploying Shiny Apps

Questions

References