Building shiny applications with R

Vaidotas Zemlys-Balevičius
2016-04-19

What is shiny?

Shiny http://github.com/rstudio/shiny is an R package which lets you to create an web application. The main benefits:

  • For R users low entry barrier

  • Majority of statistical dashboards actually require little functionality. Shiny lets to do a professional looking dashboard with minimal costs.

  • Offers a bridge to complicated Javascript visualisations via hmtlwidgets

Simple shiny app

An example can be created using Rstudio: File>New File>Shiny Web App ...

Has two parts:

  • UI
  • Server

UI overview

UI code

  • Each UI element is either input, or output and must be assigned a unique name.
  • Elements are created with function statements
  • The difference between input and output is usually evident from function name.
  • HTLM and Javascript can be inserted if needed.

Server

  • The server is a function with arguments input, output and session

  • The main shiny code which provides the actual logic of web app is the code of this anonymous function

  • All the input elements of UI are elements of input. They can be accessed with $: input$mySelect.

  • All the output elements of UI are elements of output.

  • The main goal is to connect inputs to outputs.

Server code example

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')
   })

Let the user choose a specific visualisation property or what to visualise

Reactivity

  • The most important concept in Shiny apps

  • Behind your back, Shiny builds a dependence tree on what UI element depends on what element.

  • If one element is changed, all its descendents are changed too. This is how you get interactivity

Reactivity issues

  • HTML can be faster than you, especially if you do some intense calculations in the app.

  • Shiny provides tools for finer control: observeEvent, req.

  • observeEvent: only do something if specific event is triggered

  • req: wait for the correct value

  • It still can be a struggle to get the desired result, but it was worse in previous versions of shiny.

Reactive values

  • Stash intermediate calculations into reactive values.
  • Reactive values participate in reactivity tree. Calculations are only run if parent dependency is changed.
val <- reactive({
        if(input$sel == "1") return(data1)
        if(input$sel == "2") return(data2)
        data2
    })
  • Always called as a function:
output$plot <- renderPlot({ 
        data = val()
        plot(x=data$x, y=data$y)
    })

Data sources

  • Read the data from the app directory as you do in R.
  • Connect to other data sources as you do in R.
  • Each user connecting to the app gets a copy in memory. Big local data is not advisable.

Widget ecosystem

  • Lots of nice javascript widgets ported to R: http://www.htmlwidgets.org/
  • If you are developer you can port your widget too.
  • Rstudio add-ins

Advanced developing

  • Still getting there
  • Writing all the code in server file can get tedious for big apps
  • You can use modules, recent invention.
  • You can source functions as usual.
  • No integrated unit testing. Replicate app logic in plain R and use testthat.
  • Errors give a nice traceback on recent versions of shiny. Very helpful.
  • Use browser supplied debug tools for additional help.

Deployment

  • Directly from R
  • Shiny-server. A service running on Linux. Similar to Apache. Not that hard to configure. You can find prepared instances on EC2.
  • Run from www.shinyapps.io
  • Shiny-server professional. User authorisation, more refined control.