Interactive Web Applications with Shiny

Handouts for this lesson need to be saved on your computer. Download and unzip this material into the directory (a.k.a. folder) where you plan to work.


Lesson Goals

This lesson presents an introduction to creating interactive web applications using the R Shiny package. It covers:

Data

This lesson makes use of several publicly available datasets that have been customized for teaching purposes, including the Portal teaching database.

What is Shiny?

Shiny is a web application framework for R that allows you to create interactive web apps without requiring knowledge of HTML, CSS, or JavaScript. These web apps can be used for exploratory data analysis and visualization, to facilitate remote collaboration, share results, and much more. The example below, and many additional examples, will open in a new browser window (you may need to prevent your broswer from blocking pop-out windows in order to view the app).

The shiny package includes some built-in examples to demonstrate some of its basic features. When applications are running, they are displayed in a separate browser window or the RStudio Viewer pane.

> library(shiny)
> runExample('01_hello')

Listening on http://127.0.0.1:4321

Notice back in RStudio that a stop sign appears in the Console window while your app is running. This is because the current R session is busy running your application.

Closing the app window may not stop the app from using your R session. Force the app to close when necessary by clicking the stop sign in the header of the Console window. The Console window prompt > should return.

Top of Section


Shiny Components

Depending on the purpose and computing requirements of any Shiny app, you may set it up to run R code on your computer, a remote server, or in the cloud. However all Shiny apps consists of the same two main components:

Who is “Listening” on Where?

The terms “client” and “server” show up a lot in discussing Shiny. The client is the web browser. The server is a running program that is waiting to process requests sent by the client. For development, one computer runs both the client and the server.

For big projects, the UI and server components may be defined in separate files called ui.R and server.R and saved in a folder representing the app.

my_app
├── ui.R
├── server.R
├── www
└── data

For ease of demonstration, we’ll use the alternative approach of defining UI and server objects in a R script representing the app.

# User Interface
ui <- ... 

# Server
server <- ...

# Create the Shiny App
shinyApp(ui = ui, server = server)

Hello, World!

Open worksheet-1.R in your handouts repository. In this file, define objects ui and server with the assignment operator <- and then pass them to the function shinyApp(). These are the essential components of a Shiny app.

# User Interface
ui <- navbarPage(title = 'Hello, World!')

# Server
server <- function(input, output) {}

# Create the Shiny App
shinyApp(ui = ui, server = server)

The Run App button in the Editor allows you to control whether the app runs in a browser window, in the RStudio Viewer pane, or in an external RStudio window.

The shiny package must be installed for RStudio to identify files associated with a Shiny App and provide a Run App button. Note that the file names must be ui.R and server.R if these components are scripted in separate files.

Top of Section


Accessing Data

Because the Shiny app is going to be using your local R session to run, it will be able to recognize anything that is loaded into your working environment. It won’t however find variables in your current environment! Every dependency must be in the script run by the server.

To Begin your own Shiny app, read in CSV files from the Portal Dataset, so the variables are available to definitions of both the ui and server. The app script is in the same folder as data, and you only need to specify the relative file path.

# Data
species <- read.csv('data/species.csv',
  stringsAsFactors = FALSE)
animals <- read.csv('data/animals.csv',
  na.strings = '',
  stringsAsFactors = FALSE)

Shiny apps can also be designed to interact with remote data or database servers.

Top of Section


Input/Output Objects

The user interface and the server interact with each other through input and output objects. The user’s interaction with input objects alters parameters in the server’s instructions – instructions for creating output objects shown in the UI.

Writing an app requires careful attention to how input and output objects relate to each other, i.e. knowing what actions will initiate what sections of code to run when.

This diagrams input and output relationships within the UI and server objects:

Input Objects

Input objects collect information from the user, and are passed to the server as a list. Input values change when a user changes the input, and the server is immediately notified. Inputs can be many different things: single values, text, vectors, dates, or even files uploaded by the user.

The best way to find the object you want is through Shiny’s gallery of input objects with sample code.

The first two arguments for all input objects are:

Create an input object to let users select a species ID from the species table.

# User Interface
in1 <- selectInput(
  inputId = 'pick_species',
  label = 'Pick a species',
  choices = unique(species[['id']]))

Add the input to the tabPanel() in the ui. There’s more to come for that panel!

...
tab1 <- tabPanel(
  title = 'Species',
  in1, ...)
ui <- navbarPage(
  title = 'Portal Project',
  tab1)

Use the selectInput() function to create an input object called pick_species. Use the choices = argument to define a vector with the unique values in the species id column. Make the input object an argument to the function tabPanel(), preceded by a title argument. We will learn about design and layout in a subsequent section.

Input Object Tips

Output Objects

Output objects are created by a render*() function in the server and displayed by a the paired *Output() function in the UI. The server function adds the result of each render*() function to a list of output objects.

Desired UI Object render*() *Output()
plot renderPlot() plotOutput()
text renderPrint() verbatimTextOutput()
text renderText() textOutput()
static table renderTable() tableOutput()
interactive table renderDataTable() dataTableOutput()

Text Output

Render the species ID as text using renderText() in the server function, identifying the output as species_label.

# Server
server <- function(input, output) {
  output[['species_label']] <- renderText({
    input[['pick_species']]
  })
}

Display the species ID as text in the user interface’s tabPanel as a textOutput object.

out1 <- textOutput('species_label')
tab1 <- tabPanel(
  title = 'Species',
  in1, out1)

Now the worksheet-2.R file is a complete app, so go ahead and runApp!

Render functions tell Shiny how to build an output object to display in the user interface. Output objects can be data frames, plots, images, text, or most anything you can create with R code to be visualized.

Use outputId names in quotes to refer to output objects within *Output() functions. Other arguments to *Output() functions can control their size in the UI as well as add advanced interactivity such as selecting observations to view data by clicking on a plot.

Note that it is also possible to render reactive input objects using the renderUI() and uiOutput() functions for situations where you want the type or parameters of an input object to change based on another input. For an exmaple, see “Creating controls on the fly” here.

Graphical Output

The app in worksheet-3.R, will use the “animals” table to plot abundance of the selected species, rather than just printing its id.

First, the server must filter the survey data based on the selected species, and then create a bar plot within the renderPlot() function. Don’t forget to import the necessary libraries.

# Server
server <- function(input, output) {
  output[['species_label']] <- renderText({
    input[['pick_species']]
  })
  output[['species_plot']] <- renderPlot({
    df <- animals %>%
      filter(species_id == input[['pick_species']])
    ggplot(df, aes(year)) +
      geom_bar()
  })
}

Second, use the corresponding plotOutput() function in the UI to display the plot in the app.

out1 <- textOutput('species_label')
out2 <- plotOutput('species_plot')
tab1 <- tabPanel(
  title = 'Species',
  in1, out1, out2)
ui <- navbarPage(
  title = 'Portal Project',
  tab1)

Top of Section


Design and Layout

A suite of *Layout() functions make for a nicer user interface. You can organize elements using pre-defined high level layouts such as

The more general fluidRow() allows any organization of elements within a grid. The folowing UI elements, and more, can be layered on top of each other in either a fluid page or pre-defined layouts.

Here is a schematic of nested UI elements inside the sidebarLayout(). Red boxes represent input objects and blue boxes represent output objects. Each object is located within one or more nested panels, which are nested within a layout. Objects and panels that are at the same level of hierarchy need to be separated by commas in calls to parent functions. Mistakes in usage of commas and parentheses between UI elements is one of the first things to look for when debugging a shiny app!

To re-organize the elements of the “Species” tab using a sidebar layout, we modify the UI to specify the sidebar and main elements.

side <- sidebarPanel('Options', in1)
main <- mainPanel(out1, out2)
tab1 <- tabPanel(
  title = 'Species',
  sidebarLayout(side, main))

General Layouts

The fluidPage() layout design consists of rows which contain columns of elements.

To use it, first define the width of an element relative to a 12-unit grid within each column using the function fluidRow() and listing columns in units of 12. The argument offset can be used to add extra spacing. For example:

> fluidPage(
+   fluidRow(
+     column(4, '4'),
+     column(4, offset = 4, '4 offset 4')      
+   ),
+   fluidRow(
+     column(3, offset = 3, '3 offset 3'),
+     column(3, offset = 3, '3 offset 3')  
+   ))

Customization

Along with input and output objects, you can add headers, text, images, links, and other html objects to the user interface using “builder” functions. There are shiny function equivalents for many common html tags such as h1() through h6() for headers. You can use the console to see that the return from these functions produce HTML code.

> h5('This is a level 5 header.')
<h5>This is a level 5 header.</h5>
> a(href = 'https://www.sesync.org',
+   'This renders a link')
<a href="https://www.sesync.org">This renders a link</a>

Layout Tips

Top of Section


Reactivity

Input objects are reactive which means that an update to this value by a user will notify objects in the server that its value has been changed.

The outputs of render functions are called observers because they observe all “upstream” reactive values for relevant changes.

Reactive Objects

The code inside the body of render*() functions will re-run whenever a reactive value (e.g. an input objet) inside the code is changed by the user. When any observer is re-rendered, the UI is notified that it has to update.

Question
Which element is an observer in the app within worksheet-3.R?
Answer
The object created by renderPlot() and stored with outputId “species_plot”.

The app in worksheet-4.R will have a new input object in the sidebar panel, a slider that constrains the plotted data to a user defined range of months.

in2 <- sliderInput(
  inputId = 'slider_months',
  label = 'Month Range',
  min = 1, max = 12,
  value = c(1, 12))
side <- sidebarPanel('Options', in1, in2)

The goal is to limit animals records to the user’s input by adding an additional filter within the renderPlot() function.

filter(month %in% ...)

In order for filter() to dynamically respond to the slider, whatever replaces ... must react to the slider.

Shiny provides a function factory called reactive(). It returns a function that behaves like elements in the input list–they are reactive. We’ll use it to create the function slider_months() to dynamically update the filter.

slider_months <- reactive({
    ...
    ...
  })

The %in% test within filter() needs a sequence, so we wrap seq in reactive to generate a function that responds to a user input.

slider_months <- reactive({
  seq(input[['slider_months']][1],
    input[['slider_months']][2])
})

Make sure that slider_months() is “called”, i.e. has parentheses, within the renderPlot() function.

output[['species_plot']] <- renderPlot({
  df <- animals %>%
    filter(species_id == input[['pick_species']]) %>%
    filter(month %in% slider_months()) %>%
  ggplot(df, aes(year)) +
    geom_bar()
})

The new reactive can be used in multiple observers, which is easier than repeating the seq definition again, both for you to code and for the server to process.

output[['species_table']] <- renderDataTable({
  df <- animals %>%
    filter(species_id == input[['pick_species']]) %>%
    filter(month %in% slider_months())
  df
})

Don’t forget to add a corresponding dataTableOutput() to the user interface.

out3 <- dataTableOutput('species_table')
main <- mainPanel(out1, out2, out3)

Top of Section


Share Your App

Once you have made an app, there are several ways to share it with others. It is important to make sure that everything the app needs to run (data and packages) will be loaded into the R session.

There is a series of articles on the RStudio website about deploying apps.

Sharing as Files

Sharing as a Site

To share just the UI (i.e. the web page), your app will need to be hosted by a server able to run the R code that powers the app while also acting as a public web server. There is limited free hosting available through RStudio with shinapps.io. SESYNC maintains a Shiny Apps server for our working group participants, and many other research centers are doing the same.

Top of Section


Exercises

Exercise 1

Starting from worksheet-2.R, modify the call to renderText() to create an output[['species_label']] with the genus and species name. You can find those data in the species table using the same “species_id”. Hint: The function paste() with argument collapse = ' ' will convert a data frame row to a text string.

View solution

Exercise 2

Modify the app completed in worksheet-3.R to include a tabsetPanel() nested within the main panel. Title the first tab in the tabset “Plot” and show the current plot. Title the second tab in the tabset “Data” and show a dataTableOutput(), which you can borrow from the app completed in worksheet-4.R.

View solution

Exercise 3

Use the img builder function to add a logo, photo, or other image to your app in . The help under ?img states that HTML attributes come from named arguments to img, and the “img” HTML tag requires two attributes, and you’ll probably also want to set a valide width. Hint: Use the www/images folder mentioned in the addResourcesPath() function at the bottom of .

View solution

Exercise 4

Notice the exact same code exists twice within the server function of the app you completed in worksheet-4.R: once for renderPlot() and once for renderDataTable. The server has no way to identify an intermediate result, the filtered data frame, which it could have reused. Replace slider_months() with a new selection_animals() function that returns the entire data.frame needed by both outputs. Bask in the knowledge of the CPU time saved, and congratulate yourself for practicing DYR!

View solution

Solutions

Solution 1

# Packages
library(dplyr)
library(ggplot2)

# Data
species <- read.csv('data/species.csv',
  stringsAsFactors = FALSE)
animals <- read.csv('data/animals.csv',
  na.strings = '',
  stringsAsFactors = FALSE)

# User Interface
in1 <- selectInput(
  inputId = 'pick_species',
  label = 'Pick a species',
  choices = unique(species[['id']]))
out1 <- textOutput('species_label')
out2 <- plotOutput('species_plot')
tab <- tabPanel(
  title = 'Species',
  in1, out1, out2)
ui <- navbarPage(
  title = 'Portal Project',
  tab)

# Server
server <- function(input, output) {
  output[['species_label']] <- renderText({
    species %>%
      filter(id == input[['pick_species']]) %>%
      select(genus, species) %>%
      paste(collapse = ' ')
  })
  output[['species_plot']] <- renderPlot({
    df <- animals %>%
      filter(species_id == input[['pick_species']])
    ggplot(df, aes(year)) +
      geom_bar()
  })
}

# Create the Shiny App
shinyApp(ui = ui, server = server)

Return

Solution 2

# Packages
library(dplyr)
library(ggplot2)

# Data
species <- read.csv('data/species.csv',
  stringsAsFactors = FALSE)
animals <- read.csv('data/animals.csv',
  na.strings = '',
  stringsAsFactors = FALSE)

# User Interface
in1 <- selectInput(
  inputId = 'pick_species',
  label = 'Pick a species',
  choices = unique(species[['id']]))
side <- sidebarPanel('Options', in1)
out1 <- textOutput('species_label')
out2 <- tabPanel(
  title = 'Plot',
  plotOutput('species_plot'))
out3 <- tabPanel(
  title = 'Data',
  dataTableOutput('species_table'))                 
main <- mainPanel(out1, tabsetPanel(out2, out3))
tab <- tabPanel(
  title = 'Species',
  sidebarLayout(side, main))
ui <- navbarPage(
  title = 'Portal Project',
  tab)

# Server
server <- function(input, output) {
  output[['species_label']] <- renderText({
    species %>%
      filter(id == input[['pick_species']]) %>%
      select(genus, species) %>%
      paste(collapse = ' ')
  })
  output[['species_plot']] <- renderPlot({
    df <- animals %>%
      filter(species_id == input[['pick_species']])
    ggplot(df, aes(year)) +
      geom_bar()
  })
  output[['species_table']] <- renderDataTable({
    animals %>%
      filter(species_id == input[['pick_species']])
  })
}

# Create the Shiny App
addResourcePath('images', 'www/images')
shinyApp(ui = ui, server = server)

Notice the many features of the data table output. There are many options that can be controlled within the render function such as pagination and default length. See here for examples and how to extend this functionality using JavaScript.

Return

Solution 3

# Packages
library(dplyr)
library(ggplot2)

# Data
species <- read.csv('data/species.csv',
  stringsAsFactors = FALSE)
animals <- read.csv('data/animals.csv',
  na.strings = '',
  stringsAsFactors = FALSE)

# User Interface
in1 <- selectInput(
  inputId = 'pick_species',
  label = 'Pick a species',
  choices = unique(species[['id']]))
img <- img(
  src = 'images/gkr2.jpg',
  alt = 'Portal Project research subject',
  width = '100%')
side <- sidebarPanel(img, 'Options', in1)
out1 <- textOutput('species_label')
out2 <- tabPanel(
  title = 'Plot',
  plotOutput('species_plot'))
out3 <- tabPanel(
  title = 'Data',
  dataTableOutput('species_table'))                 
main <- mainPanel(out1, tabsetPanel(out2, out3))
tab <- tabPanel(
  title = 'Species',
  sidebarLayout(side, main))
ui <- navbarPage(
  title = 'Portal Project',
  tab)

# Server
server <- function(input, output) {
  output[['species_label']] <- renderText({
    species %>%
      filter(id == input[['pick_species']]) %>%
      select(genus, species) %>%
      paste(collapse = ' ')
  })
  output[['species_plot']] <- renderPlot({
    df <- animals %>%
      filter(species_id == input[['pick_species']])
    ggplot(df, aes(year)) +
      geom_bar()
  })
  output[['species_table']] <- renderDataTable({
    animals %>%
      filter(species_id == input[['pick_species']])
  })
}

# Create the Shiny App
addResourcePath('images', 'www/images')
shinyApp(ui = ui, server = server)

Return

Solution 4

# Packages
library(dplyr)
library(ggplot2)

# Data
species <- read.csv('data/species.csv',
  stringsAsFactors = FALSE)
animals <- read.csv('data/animals.csv',
  na.strings = '',
  stringsAsFactors = FALSE)

# User Interface
in1 <- selectInput(
  inputId = 'pick_species',
  label = 'Pick a species',
  choices = unique(species[['id']]))
in2 <- sliderInput(
  inputId = 'slider_months',
  label = 'Month Range',
  min = 1, max = 12,
  value = c(1, 12))
side <- sidebarPanel('Options', in1, in2)									    
out1 <- textOutput('species_label')
out2 <- tabPanel(
  title = 'Plot',
  plotOutput('species_plot'))
out3 <- tabPanel(
  title = 'Data',
  dataTableOutput('species_table'))                 
main <- mainPanel(out1, tabsetPanel(out2, out3))
tab1 <- tabPanel(
  title = 'Species',
  sidebarLayout(side, main))
ui <- navbarPage(
  title = 'Portal Project',
  tab1)

# Server
server <- function(input, output) {
  selected_animals <- reactive({
    animals %>%
      filter(species_id == input[['pick_species']]) %>%
      filter(
        month >= input[['slider_months']][1],
        month <= input[['slider_months']][2])
  })
  output[['species_label']] <- renderText({
    species %>%
      filter(id == input[['pick_species']]) %>%
      select(genus, species) %>%
      paste(collapse = ' ')
  })
  output[['species_plot']] <- renderPlot({
    ggplot(selected_animals(), aes(year)) +
      geom_bar()
  })
  output[['species_table']] <- renderDataTable({
    selected_animals()
  })
}

# Create the Shiny App
shinyApp(ui = ui, server = server)

Return

Top of Section


If you need to catch-up before a section of code will work, just squish it's 🍅 to copy code above it into your clipboard. Then paste into your interpreter's console, run, and you'll be ready to start in on that section. Code copied by both 🍅 and 📋 will also appear below, where you can edit first, and then copy, paste, and run again.

# Nothing here yet!