8 Publishing Shiny Apps Online

This chapter will teach you how to publish your Shiny apps to three different platforms. Which one you choose will depend on the resources granted to you from your affiliation, if any.

Navigate to the section of this chapter corresponding to where you would like to publish your Shiny app. Each section will walk you through putting the app online and conclude with platform-specific exercises.

SSCC’s Shiny Server shinyapps.io Rstudio Connect
Account Needed Full SSCC membership Free account (upgradable) Paid account
Limitations One process per app (concurrent users share resources). Apps are deleted after you leave the university. 5 active apps at a time. 25 active hours per month. Connect accounts are expensive.
How to publish Publishing with the SSCC Publishing with shinyapps.io Publishing with RStudio Connect

8.1 With the SSCC

The SSCC has its own Shiny server, so that full members of the SSCC can have a place to run and publish their Shiny apps. SSCC users should keep a few things in mind:

  • Our Shiny server is limited to one process per app, which is shared across concurrent users. This means the app can be slow if multiple users are using it at the same time. It is important, then, to program efficiently and use simple datasets to make simple plots. Using large datasets, fitting statistical models, and plotting whole datasets are not good ideas.
  • Your Shiny apps will be stored on your personal Z: drive, so shortly after you leave the university, your account will be locked and the app will cease functioning. Be sure to hand the files off to somebody who will still be at the university, and update any outside links to your app with its new URL.
  • The SSCC’s packages are regularly updated, and some changes may cause your app to stop working properly.
  • Any files you want your Shiny app to use need to be made world-readable, so they will be accessible by anybody. Be sure that you are allowed to make public the data files you share.

8.1.1 Configuring Your Shiny Apps Directory

To run apps on the SSCC’s Shiny server, we need a directory called “ShinyApps” in our Z: drive, and Shiny needs to be able to read the files in this directory in order to run them. The steps below will create that directory, along with another directory for R packages, and configure the permissions so that Shiny can read them.

  1. Log on to Winstat.
  2. Open RStudio.
  3. Run this code, which will create the your ShinyApps directory and a directory for R packages if you have not already done so.
dir.create("Z:/ShinyApps")
dir.create("Z:/ShinyApps/library")
  1. Run this code, which will modify the permissions of these directories so that they are world-readable. This makes it so that the Shiny server has permission to run your apps and access any packages, data files, or any other files you place within your ShinyApps directory. (Note that this will make the Z:/ShinyApps/ directory to have the same permissions as your Z:/PUBLIC_web/ directory.)
system2("icacls", args = c("Z:/ShinyApps", "/grant", "Everyone:R", "/T"))

The system2() function will pass our code to the command line. The command we are passing to command line is icacls, or Integrity Control Access Control List, and this allows us to modify file permissions. The rest of the code specifies that we want to grant (/grant) Everyone read-access (Everyone:R) to the ShinyApps directory (Z:/ShinyApps), and that this access should apply to all subdirectories within ShinyApps (/T).

Alternatively, we could have opened the command line and supplied it with this code (icacls Z:/ShinyApps /grant Everyone:R /T), but we are running it within R for simplicity. We could also have done this through File Explorer by right-clicking on the ShinyApps directory; selecting Properties, Security, and then Edit; checking Read & Execute; and then clicking OK.

8.1.2 Configuring App Directories

Each Shiny app needs its own directory within Z:/ShinyApps/, and each Shiny app consists of either one (app.R) or two files (ui.R and server.R).

If we want to create an app called “test”, it is as simple as navigating to Z:/ShinyApps/ in File Explorer and creating a folder called “test”. (Alternatively, we could accomplish this in R with dir.create("Z:/ShinyApps/test").) Within test, we can create our Shiny app .R files:

Z:/ShinyApps/
      └─ test/
          └─ app.R

Or,

Z:/ShinyApps/
      └─ test/
          ├─ ui.R
          └─ server.R

The URLs for Shiny apps hosted at the SSCC follow this pattern:

In my case, the URL would be:

Each file and folder you create within Z:/ShinyApps/ will inherit the permissions we set earlier, to be readable by Everyone.

8.1.3 Using Packages in Shiny Apps

The SSCC Shiny server only has access to 300-some packages that it loads from a read-only location in the Linux servers. You can find the full list of packages available to Shiny below and use the search box to search the list. (This list can also be accessed by clicking here.)

The Shiny server cannot access your U: or C: drive, which is where you load packages from while in Winstat. In other words, an app that you can run in Winstat will not necessarily work when you try to run it through the web on the Shiny server, since they are loading packages from different locations.

If your Shiny app requires any packages not already available to the Shiny server (i.e., not in the list above), you will need to install them locally in your ShinyApps directory. This is why we created Z:/ShinyApps/library/. We can supply install.packages() with a custom library path. You can install any packages you need to this library with the code below, changing the package name as necessary.

install.packages("shinyWidgets", lib = "Z:/ShinyApps/library")

The structure of our ShinyApps directory is now something like this:

Z:/ShinyApps/
      ├─ test/
      │   └─ app.R
      └─ library/
          ├─ a_package_we_installed/
          └─ another_package_we_installed/

In order to tell our Shiny app to also look in Z:/ShinyApps/library/ when loading packages, we must begin each Shiny app file (app.R, ui.R, server.R) with this code:

.libPaths(c(.libPaths(), "../library"))

If we are running app.R in a directory called test/ within Z:/ShinyApps/, the path “../library” tells our app to go up one level (from Z:/ShinyApps/test/ to Z:/ShinyApps/), and then into the library/ directory (from Z:/ShinyApps/ to Z:/ShinyApps/library/). Then, in addition to looking for packages in the default paths returned by .libPaths(), our app will also check libraries installed in Z:/ShinyApps/library/.

To learn more about file paths, be sure to read our section on Paths and Working Directories from Data Wrangling with R.

8.1.4 Exercises

Check Your Understanding:

Imagine you have a directory structure as follows, where everything is world-readable:

Z:/ShinyApps/
      ├─ data1.csv
      ├─ test1/
      │   └─ app.R
      ├─ test2/
      │   ├─ app.R
      │   └─ data2.csv
      └─ library/
          ├─ package1/
          └─ package2/

Now answer the following questions:

  1. For test1/app.R to load data1.csv, which code is correct?

    • read.csv("data1.csv")
    • read.csv("../data1.csv")
  2. For test2/app.R to load data2.csv, which code is correct?

    • read.csv("data2.csv")
    • read.csv("../data2.csv")
  3. How would we load data1.csv for test2/app.R?

  4. TRUE or FALSE: For both test1/app.R and test2/app.R, the path to library/ is “../library”.

Create Your Own App:

  1. Run the code under Configuring Your Shiny Apps Directory to set up your ShinyApps directory.

  2. Create a directory within ShinyApps where you can place the files for your Shiny app. Name the folder “test” if you are not feeling creative.

  3. Install a package into Z:/ShinyApps/library/. Click here for a list of packages for extending Shiny. Use File Explorer to verify that the package installed to the correct location.

  4. Place your app.R file into your folder. View your app online at the following URL, changing “USERNAME” and “FOLDERNAME” to your values.

https://sscc.wisc.edu/shiny/users/USERNAME/FOLDERNAME/

Congratulations! You have published your first Shiny app!

8.2 With shinyapps.io

After creating a free account at https://www.shinyapps.io/, to link your account with your RStudio application,

  1. In RStudio, open the Tools menu and select Global Options.
  2. Click on the Publishing tab on the left.
  3. Click on Connect on the right.
  4. Select shinyapps.io.
  5. Follow the instructions to input your token.

After building a Shiny app, to publish it online,

  1. Open the app in RStudio.
  2. Click on the Publish icon () in the top-right of the Source pane, or the arrow next to it.
  3. Select files to be published: your app.R file and any other data files or images.
  4. Select your account.
  5. Provide a title for your app.
  6. Click Publish.

8.2.1 Editing App Timeout

Because free shinyapps.io accounts come with a 25-hour limit on app usage, we may want to decrease how long an app is active before it times out, after which point a user would need to refresh the page.

The default limit is 15 minutes, which means up to 100 app uses per month. The minimum is 5 minutes, allowing for up to 300 uses per month. To change the app timeout settings,

  1. On shinyapps.io, click on Applications on the left and then All.
  2. Click on the gear icon on the row corresponding to the app you want to modify.
  3. Click on the Settings tab.
  4. Enter a new number for Instance Idle Timeout.
  5. Click Save Settings.

8.2.2 Exercises

Follow the instructions above to publish your app.R file to shinyapps.io. View your app online at the following URL, changing “USERNAME” and “APPNAME” to your values.

https://USERNAME.shinyapps.io/APPNAME/

Congratulations! You have published your first Shiny app!

8.3 With RStudio Connect

After getting an RStudio Connect account with at least a Publisher role, to link your account with your RStudio application,

  1. In RStudio, open the Tools menu and select Global Options.
  2. Click on the Publishing tab on the left.
  3. Click on Connect on the right.
  4. Select RStudio Connect.
  5. Provide the URL for the RStudio Connect server.
  6. Follow the prompts to connect your account.

After building a Shiny app, to publish it online,

  1. Open the app in RStudio.
  2. Click on the Publish icon () in the top-right of the Source pane, or the arrow next to it.
  3. Select files to be published: your app.R file and any other data files or images.
  4. Select your account.
  5. Provide a title for your app.
  6. Click Publish.

The app name and URL can be modified online under the Access tab for your app, under Content URL.

8.3.1 Exercises

Follow the instructions above to publish your app.R file to RStudio Connect. View your app online at the following URL, changing “SERVERADDRESS” and “APPNAME” to your values.

https://SERVERADDRESS/APPNAME/

Congratulations! You have published your first Shiny app!