Module 6.2

Sharing Your App

Prework
  • Sign up for a free account on ShinyApps.io
  • Install rsconnect so that you can publish to shinyapps.io (install.packages("rsconnect"))
  • Install renv and familiarize yourself with its basic purpose and features (install.packages("renv"))

Now that you have your Shiny app, how can you share it with others? The simplest and most logical thing to do is to publish it as a web page. After all, sharing your app on the web is sort of the point of making a web app and doing so is an essential if you want to share your apps with a broader set of users who do not know how to use R or and/or Shiny. There are many different ways to publish your app but the best way when you are getting started is ShinyApps.io.

You also may want to share your app with collaborators by sending them your R script and supporting files before you make your app public or available to a broader group of users. There are many ways to share your Shiny app files but the two we are going to cover are sending the files via email or sharing them on GitHub.

Important

Note that while I am covering methods of sharing your code with others, your are not required to do so for this course. The only requirement is sharing your application as a web app via ShinyApps.io and then either writing a short essay about the app or demoing the app in a short video. Then you will submit your code via a private repo via GitHub classroom as you have been doing for the previous assignments.

Publishing your app on ShinyApps.io

Once it is ready to go, publishing your app to ShinyApps.io is super-easy. Make sure that you have the rsconnect package installed. Then, in RStudio, go to Tools > Global Options and select Publishing. Select Connect, ShinyApps.io and follow the instructions for retrieving and entering your token.

Once that is set up, open your app.R file. At the top of your Source window, you should see a little blue circle with arrows surrounding it that says “Publish the application or document” when you hover over it. Click that and, if prompted to do so, install any required updates.

Next you will be prompted for which files you want to upload. Just select the essential files that you need for the app like the app.R file and any supporting files like a .csv file with the data the app needs or helper scripts. No need to upload ancillary files like your prep file or .Rprofile.

Then hit Publish and that should be it! Here is a good set of step-by-step instructions in case you happen to get stuck.

Sharing your project folder via email

Now let’s say you want to share your code with people. The most basic way to share your app is to simply zip your project folder and send it to your collaborators via email. The other users can then place your project folder wherever they like and run the app by pressing the “run” icon or by using the runApp() function, e.g. 

# install.packages("shiny")
library(shiny)
runApp("name-of-app")

Using the renv package to record your R environment

One problem you will frequently run into with R Shiny apps is that they can break as a result of package updates and new R installations. One way to prevent this from happening is to use the renv package, which records your R environment at the time you made the app.

To do this, make sure renv is installed. Then type renv::init() in your console. Next, type ren::snapshot(). At this point, renv will give you a list of the packages and versions being used by the app. If everything looks good, type ‘y’ and renv will record the environment. When you come back to work on the app later, you can choose whether to update the packages, but if doing so breaks the app you the option of restoring the earlier environment by typing renv::restore().

Sharing your code on GitHub

Another way to share your code is on GitHub. GitHub is a platform for hosting version control repositories. In this course we will learn to use GitHub to store, manage and collaborate on code.

One central concept you want to be familiar with is a repository or “repo” for short. Repos are essentially folders where code can be stored and then accessed and changed by multiple users. All of the assignments for this course will be managed in repos.

GitHub repos are managed using a version control system named Git. Git allows developers to make and track changes to the code stored in the repo. Git also enables users to create branches to work on the code without affecting the main codebase and then merge those changes back into the main branch when they are ready.

The first thing you are going to want to do is to register a GitHub account. From there, you want to install Git and initiate Git using the usethis package. Be sure to install usethis first (install.packages("usethis")) if you don’t have it installed already. Then load usethis (library(usethis)) and type use_git_config(user.name = "Jane Doe", user.email = "jane@example.org") substituting the name and email associated with your GitHub account.

Next, you need to generate a personal access token (PAT) by typing create_github_token() and set your credentials with the gitcreds package. This is pretty simple once you have your PAT. After installing and loading gitcreds (install.packages("gitcreds"); library(gitcreds)), you can type gitcreds_get() to see whether you already have credentials set up. Then type gitcreds_set() to enter your new credentials (your username and your password, which will be the PAT that you just generated).

To create a new repo in GitHub, go to your profile, go to Repositories, select New and name your new repository. You can also give it an optional description. Make sure your new repo is set to Public and hit “Create repository.” From there copy the repo’s URL and use it to create a new version control project in RStudio. Then copy your app.R and supporting files to the new version control repo. From there you can commit and push your code to the repo on your GitHub account. We will go over these steps in class in greater detail as time allows.