Interactive data visualization: shiny and ggvis

Mahbubul Majumder, PhD
Nov 4, 2014

What is shiny ?

  • A web application framework for R

    • can create data driven web application
    • knowledge of HTML, JavaScript and CSS are not necessary
  • Can be used to create data product

  • D3 plots can be included in shiny application

  • Can be easily shared in various ways

    • deploy on RStudio server
    • deploy on your own website
install.packages("shiny")
library(shiny)
  • Click here to view some example

How shiny works

  • You need two R source files saved in a folder say myShinyApp. Source files should be named

    • server.R
    • ui.R
  • server.R contains the R codes to

    • generate the plots
    • present tables of results
  • ui.R contains the R codes to

    • design the user interface for facilitating the user interactivity with the plots
    • to add typical widgets such as slider, check box, select box etc.
  • Two ways to run your application

    • from command prompt type runApp("path-to/myShinyApp")
    • from RStudio, click on Run App

A typical server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  # Expression that generates a histogram. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot

  output$distPlot <- renderPlot({
    x    <- faithful[, 2]  # Old Faithful Geyser data
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
})

A typical ui.R

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

Available widgets

functions widget
actionButton Action Button
checkboxGroupInput A group of check boxes
checkboxInput A single check box
dateInput A calendar to aid date selection
dateRangeInput A pair of calendars for selecting a date range
fileInput A file upload control wizard
helpText A Help text that can be added to an input form
numericInput A field to enter numbers
radioButtons A set of radio buttons
selectInput A box with choices to select from
sliderInput A slider bar
submitButton A submit button
textInput A field to enter text

Application layout

  • Example of a navigation bar navbar

  • example of tabsets tabsets

Application theme

  • The default shiny theme is bootstrap which is usually loaded from downloaded bootstrap.css
shinyUI(fluidPage(theme = "bootstrap.css",

  titlePanel("My Application")

  # application UI              
))
  • Some themes can be viewed from

Demonstration

  • Let us create an application using shiny

  • We will create an application to explore US crime data from 1969 to 2005

  • The application that we made is already hosted on Github. To launch this application, run the following codes from R command prompt

library(shiny)
shiny::runGitHub('usa-crime', 'mamajumder')

Interactive visualization using ggvis

  • Some interactive plots can be generated using package ggvis
    • still being developed
    • can be used in shiny application
    • similar to ggplot2 but designed for dynamic web graphics
    • uses chain operation %>% for multiple layers
install.packages("ggvis")
library(ggvis)
  • User inputs are passed through some input functions

ggvis input functions

  • Input functions are
    • input_slider(): slider input
    • input_checkbox(): a check-box
    • input_checkboxgroup(): a group of check boxes
    • input_numeric(): a spin box
    • input_radiobuttons(): pick one from a set options
    • input_select(): create a drop-down text box
    • input_text(): arbitrary text input
  • Example
mtcars %>% ggvis(x= ~wt) %>%
  layer_densities(
    stroke := input_radiobuttons(c("Purple","Orange","steelblue"), 
                                 label="Line color"),
    fill := input_select(c("Purple","Orange","steelblue"), 
                         label="Fill color")
    )

Example: ggvis

library(ggvis)
mtcars %>% ggvis(x = ~wt) %>%
    layer_densities(
      adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
      kernel = input_select(
        c("Gaussian" = "gaussian",
          "Epanechnikov" = "epanechnikov",
          "Rectangular" = "rectangular",
          "Triangular" = "triangular",
          "Biweight" = "biweight",
          "Cosine" = "cosine",
          "Optcosine" = "optcosine"),
        label = "Kernel")
    )
  • ggvis can do limited things. But what it does, it requires less effort.
    • For more control use shiny

Reading assignment and references