R Shiny - Getting your data/analysis onto the web with Shiny apps.

A few years ago a I worked in a lab that was spending a lot of money using commercial statistical software to analyse Phytoplankton physiological data collected with a PAM and a group of PhD students, including me, decided that we could do the same analysis using the free and open statistical programming language R. We produced a set of functions to do all of the analysis needed by the team but after a while we realised that people were not using them as they weren’t comfortable with the command line or with R. Therefore we set about building a simple Graphical User Interface designed specifically to use our R analysis tools using a Shiny app. Here are a few notes on how to get started in Shiny and a link to the PAM Light Curve analyser we built.

What is it?

The Shiny CRAN page has this to say: “shiny: Web Application Framework for R makes it incredibly easy to build interactive web applications with R. Automatic “reactive” binding between inputs and outputs and extensive pre-built widgets make it possible to build beautiful, responsive, and powerful applications with minimal effort.”

But to me Shiny’s greatest value is that there’s no need to re-invent the wheel in html or JavaScript as a lot of the components that you want for a simple app have already been done before and can be copied directly from others. This is super easy in Shiny as they have a great Gallery of examples - http://shiny.rstudio.com/gallery/

For some inspiration see the User Showcase: https://www.rstudio.com/products/shiny/shiny-user-showcase/

Here is an embedded example from the Shiny Gallery:

How to Shiny?

First of all, there are lots of different ways to start using Shiny but the best way is to take half an hour and work through the on-line tutorial here: http://shiny.rstudio.com/tutorial/.

Structure of a Shiny app

Basically, there are two components to a Shiny app: 1) a user-interface script (ui.R), and 2) a server script (server.R). The ui.R script controls the layout and appearance of your app. The server.R script contains the instructions that your computer needs to build your app, including loading any data and or doing any analysis or plotting etc that you want to do.See the tute for an example of each

Running a shiny app

As all Shiny apps have the same basic structure: two core R scripts saved together in a directory. The easiest way to run your app is to make a new directory and saving a ui.R and server.R file inside it. Each app will need its own unique directory.

You run a Shiny app by giving the name of its directory to the function runApp in R. For example if your Shiny app is in a directory called my_app, run it with the following code inside an R shell:

> library(shiny)

> runApp("my_app")

Sharing the Shiny - Hosting and Deployment

Up to this point everything you’ve been doing is probably isolated on your own computer and not shareable or on the internet. You can, as I often do, just use these shiny apps as simple GUI interfaces to your analysis and share them with your friends on Git Hub or if you want to you can put your Shiny app on the web for your friends to use. To do this you can choose to deploy on your own R Shiny servers or use the RStudio Shiny hosting service for a small fee.

The PAM Processor App

This is a project that allows users to import their PAM fluorometer data into the R programming language and calculate photosynthetic parameters as per Platt et al 1980 and Ralph et al.

This code is brought to you by Rob Johnson, Simon Reeves, Shihong Lee, and Emma Flukes. We are (or were) PhD students at the University of Tasmania and we were fed up with the ridiculous amount of time and money it was taking to analyse PAM Rapid Light Curves using clunky and expensive stats software (copy… paste… copy… paste… SPSS… boooo!). So, we built the PAM Processor! using the free and open stats scripting language R and the web application tool Shiny.

At the moment this is release v1.0 of the PAM Processor! as of June 2015 and it’s available for download here: https://github.com/RobTheOceanographer/pam_in_R/releases

Written on September 3, 2015