September 29, 2016

Outline

  • Motivation

  • Shiny

  • Reactive Programming

  • Demo/Examples

  • Resources

Motivation

R is great!

Powerful and flexible tool designed for statistical computing and graphics

  • Leading software for statistics

  • Free and opensource

  • Excellent for data visualization

  • Large and enthusiastic community

But …

  • A personal experience, not a shared one

  • You choose what output to present

  • Output is in a static format

  • Modern visualization is interactive, browser based

Goal

  • "R is great!"

  • "The internet is great"


Let's combine them

Shiny

What is shiny?

"Shiny is an R package that makes it easy to build interactive web applications straight from R"

– Rstudio –


  • Open-Sourced R package by RStudio (11/2012 on CRAN)

  • New model for web-accessible R code

  • Able to generate basic web User Interfaces (UIs)


  • Zero HTML, CSS, and JavaScript knowledge is required

  • … but it is customizable and extensible with HTML, CSS, and JavaScript

  • It is based on reactive programming: i.e. it simplifies code as compared to standard for UI or web programming

Reactive programming

How does reactive programming work?

  • a = 3

  • b = a + 2

  • a = 7

  • b == ?


Imperative: b = 5

Reactive: b = 9

Demo/Examples

Demo

## Load data
data("faithful")

## Interested in "Eruption time in mins" (x)
x = faithful[, 2]

# Exploring possibilities for drawing an histogramm
hist(x)
hist(x, breaks = 9)
hist(x, breaks = seq(from = 40, to = 100, by = 10))

# draw final histogram
bins = seq(min(x), max(x), length.out = 9)
hist(x, breaks = bins, col = 'darkgray', border = 'white')

# Define UI for application that draws a histogram
ui <- fluidPage(
   
   # Application title
   titlePanel("Old Faithful Geyser Data"),
   
   # Sidebar with a slider input for 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")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   
   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      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')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

Examples

Resources

Resources