Creating Interactive Visualisations Using R and Shiny

Martin John Hadley
Academic Research Technology Specialist
University of Oxford

Code for talk available here, https://goo.gl/pU5tu4

What do you now about the R data visualisation stack?

R

R is a scripting language and a very powerful tool for data analysis and presentation, primarily due to the huge user base and their dedication to developing free and open source libraries/packages covering a vast range of different knowledge domains:

R provides an excellent workflow for reproducible research; combining code with data, textual explanations and interactive data visualisations.

Who’s used RStudio?

RStudio/

RStudio/

RStudio is a free, open-source IDE (integrated development environment) that provides an extremely powerful and friendly interface for developing with R.

RStudio is also the name of the developers of RStudio - they have developed an impressive stack of technologies for making R easier to use and more powerful for reproducible research and creating interactive data visualisatons.

Some of the technology they’ve built includes:

RStudio/

RStudio/
  • The “tidyverse” is a collection of packages maintained by RStudio devs [particularly Hadley Wickham]
  • tidyverse packages play extremely nicely together
  • tidyverse packages are extremely useful for preparing data for interactive visualisations
  • tidyverse packages are highly optimised, often specifically around nitpicky details of bse R (readr is a good example of this)
  • tidyverse is the backbone of the recently published, free online book R for Data Science

Who’s heard of RMarkdown?

RStudio/

RMarkdown/

RMarkdown allows for code, text, images and interactive data visualisations to be combined together in the same document.

This presentation was built using RMarkdown and published to the free hosting platform RPubs.

library(leaflet)
africa_data_points = data.frame(
  lat = rnorm(26, mean = 6.9, sd = 20),
  lng = rnorm(26, mean = 17.7, sd = 20),
  size = runif(26, 5, 10),
  label = letters
  )
leaflet(data = africa_data_points) %>%
  addTiles() %>%
  addCircleMarkers(popup = ~label, radius = ~size)

Who’s heard of htmlwidgets?

htmlwidgets

htmlwidgets is a framework for binding R to a JavaScript library of choice, and to bundle this functionality into an easy to use library:

library(leaflet)
leaflet() %>%
  addTiles()

The htmlwidgets.org website provides an overview of some of the most popular htmlwidgets:

RStudio/

Interactive tables:

Interactive ggplot2

If you use ggplot2 and are not going overboard it’s probably you can directly covert your charts into interactive charts using the plotly library:

ggplot2: Interactive Points and Ribbons

## verbatim from https://plot.ly/ggplot2/geom_ribbon/
library(ggplot2) 
p1 <- ggplot(new.dat,aes(x=x,y=pred)) + 
geom_line() +
geom_ribbon(aes(ymin=pred-2*SE2,ymax=pred+2*SE2),alpha=0.2,fill="red") +
geom_ribbon(aes(ymin=pred-2*SE,ymax=pred+2*SE),alpha=0.2,fill="blue") +
geom_point(data=dtfr,aes(x=x,y=y), size=1) +
scale_y_continuous("y")
p1

library(plotly)
ggplotly(p1)

ggplot2: Interactive Gantt Chart

Is that all you need?

If you’re interested in building interactive maps, charts, networks with the ability for users to zoom/pan/filter* then these are the only tools you need:

Where can I learn more?!

Training here: 14th and 21st November 14:00-17:00

Lynda course: Creating Interactive Presentations with Shiny and R

What if that’s not all you need?

You might be exciting and want to have multiple [interdependent] charts, or for users of your visualisations to see additional information when they select data.

Shiny

Shiny is a web framework developed by RStudio that allows impressively interactive web applications to be created using R.

There’s no need* to learn HTML or JavaScript to build these interactive apps.

But these are complicated right?

But these are complicated right?

Beamer/

But these are complicated right?

Beamer/

How about htmlwidgets and Shiny?

Hosting these is complicated and expensive right?

Expense is related to complexity, so let’s answer that first

Shinyapps.io

shinyapps.io is a hosted solution for sharing Shiny apps on the web, run by RStudio.

There’s a free tier of the service available that you can sign up for right now.

You can deploy 5 interactive applications for free, with a total of 25 interactive hours per month.

Interactive Hours?

All accounts have a limited number of “active hours” - this is the time during which an app is running.

When starting off with Shiny you’re not going to hit 25 hours per month, experiment freely.

If you start hitting up against the 25 hours - the University is investing in a shinyapps.io subscription [and later a full service]

Deploying these is complicated though?

Publishing your Shiny app to shinyapps.io is incredibly simple if you’re using RStudio, simply click on the publish button.

Beamer/

More examples of Shiny apps?

The Live Data project has been funded by IT Services to support researchers in creating interactive data visualisations, during this project we’ve learned how impressive [and easy to use] Shiny is for building visualisation case studies.

These are two of the visualisations we’ve built that I want to particularly highlight:

Beamer/

Beamer/

Things you might not believe are shiny apps

Built by Dean Attali:

Where can I learn more?

There is a step-by-step guide to learning Shiny produced by RStudio here: http://shiny.rstudio.com/tutorial/. This is how I originally learned Shiny in May 2015, it’s a good course but may assume some knowledge you don’t have.

But there are other online tutorials available, in fact Dean Attali has a great tutorial on hosting your own Shiny server.

Training here: 14th and 21st November 14:00-17:00

Lynda course: Creating Interactive Presentations with Shiny and R