shiny applicatons are a GUI (graphical user interface) that can run a R-based application. You can then post this online and it will run from an R server (not your computer). Users can run your application from the web without having to install R. In fact they can run from any browser, for example a smart phone. Alternatively, you can just have it in a package and provide it as a way for users to run analyses when they use your package from R.

Fisheries examples

Here are two example shiny apps that we have at the NWFSC. They are simple but are intended mainly to allow users to run standard analyses or explore data. shiny applications can be quite sophisticated and ‘shiny’. Search online for ‘shiny demos’.

The U.S. Geological Survey (a U.S. federal agency) also it active in developing tools to be used by the public and serves these on its usgs-r.github.io organizational site. They have been active in using Shiny apps for data exploration.

Make your first shiny app

  1. Install the shiny package.
install.packages("shiny")
  1. Click File > New > Shiny Web App…
  2. Select a location to put the application folder.
  3. Select “Multiple File”. You can choose single file, but I will demo the separate case with a separate file for the interface code and ‘workhorse’ code.
  4. Give you application a name.
  5. When the files ‘ui.R’ and ‘server.R’ open, click ‘Run App’.

That’s it. You made a shiny application. To stop the app, close the window or hit ESC at the command line in RStudio.

ui.R file

This is the code that specifies the graphical interface.

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
       sliderInput("bins",
                   "Number of bins:",
                   min = 1,
                   max = 50,
                   value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )
  )
))

server.R

This part has the R code that does the analysis, makes plots, makes table, etc.

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

What can shiny apps do and how can I copy that?

Go to the RStudio shiny gallery, scroll down to the “Start Simple” section and start there. Once you want to add some more sections to your app, look lower at the Widgets sections. The code for the examples is there.

You’ll also want to go through RStudio’s online tutorial for shiny apps.

Development of anything past a fairly simple shiny app is quite time-consuming. This is a task that I outsource.

Putting your application online

NW Fisheries Science Center (where I work) has its own R Shiny Server because we has applications with proprietary data that cannot be put on non-NOAA server. In addition, many of our applications require specialized extra software for fitting Bayesian models. Thus our application need more than just R packages.

However, if your application does not have these special constraints, you can host online for free with http://www.shinyapps.io/.

Follow the instructions here. In summary:

  1. Sign up for an account at http://www.shinyapps.io/

  2. install the rsconnect package

install.packages("rsconnect")
library(rsconnect)
  1. Login into your account at http://www.shinyapps.io/. Click on your user name in top right, and click ‘Tokens’

  2. A window should popup with code that looks like

rsconnect::setAccountInfo(name="<ACCOUNT>", token="<TOKEN>", secret="<SECRET>")

Click ‘Copy to clipboard’.

  1. Paste that code into the command line in R.

  2. From R, click on the ui.R or server.R file. Click ‘Run App’. You should see a ‘Publish’ button in the top right. Click that. Select ‘shinyapps.io’ and log into your account.

  3. When your app opens, copy the url to the app.

Adding a shiny app to your R package

  1. In your DESCRIPTION file, add ‘shiny’ to the list of packages in the ‘Imports:’ line.

  2. Create a folder called ‘inst’ in your package.

  3. Create a folder called ‘shiny’ in that.

  4. Within that folder, put your folder with all your application files. ‘ui.R’, ‘server.R’, any data files, any function files.

  5. Add the following code to a files called launchApp.R to your R directory. Change “myapp” and “mypackage” to the right names.

#' @export
launchApp <- function(x="myapp") {
  appDir <- system.file("shiny", "myapp", package = "mypackage")
  shiny::runApp(appDir, display.mode = "normal")
}

Rebuild your package. If later you add more shiny apps, but their folder in the shiny directory too. You run them with launchApp("mynewapp").

  1. To run
library(mypackage)
runIt("myapp")

Replace ‘mypackage’ and ‘myapp’ with the right names.