Interactive graphics

  • an animated graphics is dynamic but not interactive
  • on the other hand, in interactive graphics the user can directly interact with the plots, for instance by changing the number of bins of an histogram in real-time
  • interactive graphics is realized in R in two complementary ways: HTML widgets and Shiny

HTML widgets

HTML is an interactive format, and you can take advantage of that interactivity with HTML widgets, R functions that produce interactive HTML visualisations

For example, take the leaflet map below:

library(leaflet)
leaflet() %>%
  setView(174.764, -36.877, zoom = 16) %>% 
  addTiles() %>%
  addMarkers(174.764, -36.877, popup = "Maungawhau") 

HTML widgets

  • the great thing about HTML widgets is that you don’t need to know anything about HTML or JavaScript to use them. All the details are wrapped inside the package, so you don’t need to worry about it
  • HTML widgets provide client-side interactivity — all the interactivity happens in the browser, independently of R
  • that’s great because you can distribute the HTML file without any connection to R
  • however, that fundamentally limits what you can do to things that have been implemented in HTML and JavaScript

Shiny

  • an alternative approach is to use shiny, a package that allows you to create interactivity using R code, not JavaScript
  • shiny interactions occur on the server-side. This means that you need a server to run them on
  • when you run shiny apps on your own computer, shiny automatically sets up a shiny server for you, but you need a public facing shiny server if you want to publish this sort of interactivity online
  • that’s the fundamental trade-off of shiny: you can do anything in a shiny document that you can do in R, but it requires someone to be running R
  • you can share online your shiny apps using shinyapps.io

Shiny

Let’s reveal the basics of shiny:

  • helloShiny just defines the user interface and the server logic functions
  • helloWidgets showcases the possible widgets that allow interaction with the app
  • helloReactivity shows how to create reative outputs reading the input of widgets

Dashboards

  • Dashboards use R Markdown to publish a group of related data visualizations as a dashboard
  • they are flexible and easy to specify row and column-based layouts
  • components are intelligently re-sized to fill the browser and adapted for display on mobile devices
  • they support both HTML widgets as well as shiny