Interactive Data Visualization with R Shiny

Wenjie Wang
Department of Statistics, UConn

January 30, 2018

Getting Started

What is R Shiny?

Selective examples

Structure of a Shiny app

By execution order:

  1. global.R: an optional script for code needed in ui.R and server.R

  2. ui.R: define user interface (UI) design

  3. server.R: define server-side logic

Alternative structure

  • a sinlge script called app.R

HTML builders

Shiny HTML5 creates
p() <p> A paragraph of text
h1(), …, h6() <h1>, …, <h6> a first, …, sixth level header
a() <a> A hyper link
br() <br> A line break
div() <div> A division with a uniform style
span() <span> An in-line version of division
strong() <strong> Bold text
em() <em> Italicized text
HTML()   Directly passes character strings as HTML
  • shiny imports HTML builder functions from htmltools.
  • names(tags) returns a complete valid HTML5 tag list.

Basic widgets

  • What is a web widget? A web element that users can interact with.
  • Standard widgets gallery
  • The first two arguments for each widget function are
    • id for widget name: users will not see the name, but you can use it to access the widget’s value. The name should be a character string.
    • label for widget label: this label will appear with the widget in your app. It should be a character string, but it can be an empty string "".

The standard Shiny widgets include

function 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 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

Reactive output

*Output functions in ui or ui.R turn R objects into output of UI.

Output function Creates
htmlOutput raw HTML
imageOutput image
plotOutput plot
tableOutput table
textOutput text
uiOutput raw HTML
verbatimTextOutput text
  • These *Output functions take output name/ID as input.

render* functions in server or server.R

render function creates
renderImage images (saved as a link to a source file)
renderPlot plots
renderPrint any printed output
renderTable data frame, matrix, other table like structures
renderText character strings
renderUI a Shiny tag object or HTML
  • These render* functions take a single argument: an R expression surrounded by {}.
  • Shiny runs the R expressions inside render* functions once each time a user changes the value of a widget.

Reactive expressions

  • Create a reactive expression by the reactive() function, which takes an R expression surrounded by {} similar to render* functions.
  • Reactive expressions cache values and update them only when it is necessary, which make the app faster.

Share Shiny apps

  • share as R scripts by runApp(), runUrl(), runGitHub() or runGist()

  • share as a web page:

More advanced HTML widgets

  • htmlwidgets package provides a framework that helps to bring the best of JavaScript data visualization to R.
  • example R packages built with htmltwidgets:
    • leaflet for geo-spatial mapping powered by JavaScript library leaflet
    • dygraphs for time series charting powered by JavaScript library dygraphs
    • plotly and Highcharter for general interactive graphics powered by JavaScript library plotly.js and Highcharts, respectively.
    • DT for tabular data display powered by JavaScript library DataTables.

leaflet example

  • locations of earthquakes off Fiji

dygraphs example

plotly example

DT example

More options on UI and control widgets

Further reading


  • a cool plate found at (UConn) Y lot in last Summer