Introduction to Shiny Workshop

David Mawdsley, Louise Lever, Joshua Woodcock

https://uomresearchit.github.io/r-shiny-course/

Shiny for R

  • Housekeeping
  • Introduction to Shiny
  • Gapminder data
  • Running example: visualising the gapminder data
  • Deploying Shiny apps

Housekeeping

  • Fire exit
  • Toilets
  • Coffee breaks and lunch
  • Course timing
  • Feedback (form and verbal)

Research IT

Described on the IT Services website

Announcements given via the Research IT blog and newsletter (sign up via the blog page) and on Twitter [@UoM_eResearch](http://twitter.com/uom_eresearch)

  • Training courses teach computing skills for research
  • Advice and guidance about research software
  • Access to specialist support and consultancy e.g. code reviews
  • Access to HPC systems - where you can run R.
  • Full list of services on offer

Get in touch via the support centre

R user groups

R user groups are a great way of finding out more about R.

  • University of Manchester R user group
    • Meets monthly
    • Email list for announcements, questions etc.
    • Has tea, coffee and biscuits
    • Separate beginners’ group
  • ManchesterR
    • City-wide group (not affiliated with the university)
    • Meets quarterly
    • Typically 3-4 presentations; often a commercial focus.

Introduction to Shiny

Why (not?) use Shiny

  • Fast to develop
  • Use R’s libraries easily -including e.g. leaflet for mapping
  • Many built in widgets
    • And packages for others

  • Awkward to scale
  • Non-free pro version needed for some server features

Gapminder

Gapminder Video

Workshop Aims

  • To use Shiny to make an interactive plot
    • Select / animate the year
    • Only plot data for selected continents
    • Deploy the app to shinyapps.io
    • (Possible extensions included in “going further”)

Getting started

  • The course builds on a single worked example.
  • Download the zip file
  • Decompress this to a directory within C:\work
  • Each step is separate labelled folder, which is cross referenced in the notes, e.g. 01_defaultapp.

  • The course website https://uomresearchit.github.io/r-shiny-course/ contains all the exercises, links to solutions and more detail than these slides.

Interactive exercises with git

If you’re happy using git:

git clone https://github.com/UoMResearchIT/r-shiny-course-materials.git
  • Solutions are stored as tagged commits.
    • e.g 01_defaultapp
    • Link takes you to github’s commit page
    • Checkout versions with git checkout 01_defaultapp
  • Make your own Shiny app in another directory

Getting started (2)

  • Run rstudio:
rstudio
  • We’ll make a new project for our app
    • Make this in C:/work and copy to your P drive at the end of the course
    • When we make a Shiny Web Application project, we get a simple Shiny app for free.

Getting started (3)

  • Copy the following files from the zip file to your project directory:
    • gapminder.rds contains the gapminder data
    • workshopFunctions.R contains the functions we will use.
      • These are mostly to make it easier to make the plots, and to avoid this turning into a ggplot workshop.
    • codeExample.R shows how to use the plotting functions.
  • Run App using the toolbar or Ctrl+Shift+Enter

01_defaultapp

Anatomy of a Shiny App - user interface

ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins", ...
        ),
        
        
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

input$bins is a source

output$distPlot is an endpoint

Anatomy of a Shiny App - server

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

Shiny handles the dependencies between sources and endpoints, and only updates what is needed - Reactive programming.

Exercise - getting ready for the rest of the course.

  • Copy the gapminder data (gapminder.rds) and workshopFunctions.R to your app directory
  • Modify the app to load the ggplot2 and dplyr libraries, gapminder data, and workshopFunctions.R

  • Check your app still works

02_loaddata

(Course notes: Getting started)

More widgets

Exercise

  • Add a checkboxGroupInput() widget to select the continents we wish to display on the graph
  • Change the title of the app in titlePanel() to something sensible

(This won’t do anything yet - we’ll connect it to the graph shortly)

03_continentwidget

(Course notes: User interface design)

Exercises - Putting it all together

  • Replace the histogram with a gapminder plot, using produceGapminderPlot() 04_gapminderplot
    • See codeExample.R
  • Replace the bins widget with a widget to select the year 05_yearwidget
  • Use the outputs of the widgets to make the graph interactive 06_interactive

(Course notes: Putting it all together)

Deploying Shiny apps

  • Run from R(Studio)

  • https://shinyapps.io
    • Easy deployment from RStudio
    • Various pricing tiers (inc. free)
  • Shiny server
    • Free and commercial versions
    • Authentication requires commercial version

(Course notes: Deploying Shiny apps)

Summary

  • Define the UI and server functions
  • Reactive programming handles the depencencies between elements
  • Deploy apps using shinyapps.io or to a Shiny Server

Extra Slides

App layout

  • The default apps sidebarLayout() is a good starting point.
  • The fluidPage() layout gives us more control.
  • The widths of each row of columns should sum to 12 15_layout