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.
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.
When R-Studio appears, click on Tools>Terminal>New Terminal. This will bring up a command line window.
In this window, type the following command to clone the workspace to your R-Studio environment:
git clone https://github.com/ENV859/ENV859_GIS_RBefore 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,.
In the lower right corner of your R-Studio session, click on the Files tab. You should see the ENV859_GIS_R folder. Open this folder by clicking on it.
Click on the InstallPackages.R script in the Files window. This will open the R script in RStudio.
Highlight all the text in the file and click the Run icon at the top of the window (or hit ctrl-enter)
Yes if it asks to restart your R sessions prior to installing.Under the File menu, select New File>New Shiny Web App...
In the window that appears:
MyFirstAppSingle file (app.R)This will add a new file, app.R to your session.
app.R script active, click Run App button in the Script window. This will open your app in a browser-esque window. Close your application window. (Alternatively, find and click the small stop sign in RStudio).
In the app.R document:
shiny library, adding R/Shiny functionality to your script.ui. More on this in a bit...server. More on this in a bit...shinyApp command, passing the ui and server functions as parameters, which loads the shiny web app in a browser. A Shiny app is essentially composed of two functions, the ui and the server function. So let's talk more about them.
ui functionThe 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:
> ?fluidPageThe 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.
server functionThe 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.
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 .
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:
Files section of your R-studio session (lower right corner), navigate into the ENV859_GIS_R folder, if you're not there already. ENV859_GIS_R.Rproj document to open the project. (No need to save the current workspace image...)Lesson3_Shiny/Example3 folder in the Filessection and open both the ui.R and server.R scripts by clicking on them. ui and the server functions.