Getting started with R/Shiny (2017)

1. Set-up

Opening up your RStudio container

We'll be using Duke's R-Studio container for our work. This is so you can continue to play with the session for the remainder of the semester (and longer) without needing to re-install libraries.

Downloading materials to your container

Next, we need to download the materials for the session. These are stored on a GitHub repository, which we can clone to our R-Studio container.

Installing libraries

Before we get going with RShiny, we'll need to install required libraries on our R-studio container. This only needs to be done once, but is ok to run if you've done it before,.

2. The basic R/Shiny application

Create your first RShiny application

This will add a new file, app.R to your session.

Run your first R/Shiny application
Investigate the R script that designs the app

A Shiny app is essentially composed of two functions, the ui and the server function. So let's talk more about them.

The ui function

The uifunction defines the layout of the shiny app, i.e., what appears when we open the app. If you run the app again, you see three elements in the web page: a title ("Old Faithful Geyser Data"), a slider ("Number of bins:"), and the histogram. Now, if you return to the ui function, you see each of these defined: titlePanel, sliderInput, and plotOutput, respectively. There are also some other elements, sidebarLayout and sidebarPanel, which are used to organize the page, and these are all stored within a fluidPage document.

More information on any of these elements can be viewed by typing the name preceded by a ? in the console window:

The two interactive elements in the page, the sliderInput and plotOutput , both have names associated with them: bins and distPlot, respectively. This is important as this enables interaction between the ui and the server functions.

The server function

The server function is the engine of our application. It takes the information held in the elements defined in the ui function and performs some action on them. In this sample app it simply generates a histogram of some data using the number of breaks set in the ui's bin object (line 44). But let's explain how exactly this works...

The server function has two parameters: input and output, both of which have hooks to the user interface, i.e., the ui function, of our Shiny app. The input parameter contains references to user interface objects that pass data into the server function, and the output parameter contains references to user interface objects that receive data from our server function.

For example, in line 41, where the bins variable is set (a prerequisite for creating the histogram), the length.out item is set to input$bins. This means that it's getting that value from the bins object defined in the ui function, which is our slider (line 21).

Also, you see that, in line 38, the histogram created is rendered into the ui object named distPlot via the output$distPlot syntax.

Quick recap

Certainly there's a bit more going on here and some more R syntax to learn, but the gist of R/Shiny apps is revealed: We define the structures we want to add to our user interface via the ui function and link the actions we want to run with the values manipulated there via the server function.

Another format for R/Shiny apps is, rather than a single script holding both the ui and server functions, is to separate them into individual documents named ui.R and server.R .

3. More complex examples

The repository we cloned earlier has the above example as well as two more complex examples to view and play with. We can access these by first opening the R-project associated with these (and other) examples: