How to Share R Shiny Apps – Top 3 Methods Explained

[This article was first published on r – Appsilon | Enterprise R Shiny Dashboards, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Let’s face the facts – you want your amazing dashboards seen by the world. To do so, you have to find a way to share R Shiny apps. There are numerous ways you can approach this, from sharing your code as a zip file to Docker images. We’ll cover a few of the possibilities today, and leave edge cases for some other time.

Interested in R beyond Shiny? Learn to make PowerPoint presentations with R Markdown.

Table of contents:


Let’s Code a Basic R Shiny App

This section should feel easy even if you know nothing about R Shiny. We’ll create a simple web application that lets users enter their names, and Shiny then greets them with a custom hello message. Think of it as a “Hello World” for R Shiny.

Open RStudio and create a new file called app.R. Then paste the following code:

Here’s what you’ll see once you run the app:

Image 1 - “Hello World” R Shiny app

Image 1 – “Hello World” R Shiny app

To recap, the app has a single input and a single output element, and both are text-based. It’s nothing to write home about but will serve us just fine to explore sharing options.

We have a solution if you want something more advanced, but don’t have the time to build it. You can download several of Appsilon’s Shiny Templates for free. Then you can customize your new Shiny templates in less than 10 minutes with our guide on developing Shiny dashboards.

How to Share R Shiny Apps on GitHub

You have two options if you want to share R Shiny apps through GitHub:

  1. As a repository – Recommended approach, especially if your app is broken into multiple files. The best practice is to include a readme file to instruct the reader on how to get started.
  2. As a gist – Only for a single-file R Shiny apps, like ours is. You can load the Shiny application directory from a GitHub gist in R.

There are other, more advanced options, such as GitHub actions, but it’s out of the scope for today’s article.

Our app only has a single file (app.R), so let’s proceed with gists. Log in to your GitHub account and select a New Gist option under the plus sign in the top right corner. Add the name and code to the gist:

Image 2 - Creating a new GitHub gist

Image 2 – Creating a new GitHub gist

Change the bottom dropdown value to “Create public gist” and click on that button. Your gist will be created immediately. The URL will have the following structure: gist.github.com/<your_username>/<gist_id>. The last part (<gist_id>) is all we need.

Create a new R file in RStudio and paste the following code:

Of course, you should replace the value in runGist() with your gist ID. Once done hit CTRL/CMD + Enter to run the code line:

Image 3 - Running an R Shiny app from a GitHub gist

Image 3 – Running an R Shiny app from a GitHub gist

And there you have it. It’s pretty basic and limited but works for simple apps. The major downsides are that other users are assumed to have R and RStudio installed, and you can only share single-file Shiny apps this way.

The next option to share R Shiny apps addresses these shortcomings.

ShinyApps.io – Share your R Shiny Apps for Free

The term “free” is open for debate. You can host your Shiny apps free of charge as long as you have 5 or fewer applications and don’t need more than 25 active hours. If you need to scale, the paid plans start at $9 per month.

The free plan is enough to serve our needs. You’ll have to register for a free account first. Use your Google or GitHub credentials to shorten the process. Once done, you’ll have to install the rsconnect package from the R console and authenticate through it. The authentication details (name, token, and secret key) are listed on the shinyapps dashboard page:

From the console, import the rsconnect package and call the deployApp() function as shown below:

The deployment process might take a minute. Here’s what you should see printed to the console:

Image 4 - Deploying a Shiny app to ShinyApps.io

Image 4 – Deploying a Shiny app to ShinyApps.io

The good news? The app will open automatically once deployed. Here’s what it looks like on our end:

Image 5 - Deployed R Shiny app on ShinyApps.io

Image 5 – Deployed R Shiny app on ShinyApps.io

It looks identical to the one running locally, but the URL tells us that we’re looking at the deployed version. You can now share this URL with anyone and they can easily interact with your app!

But can we take it a step further? Yes, by managing the entire server on our own. Let’s see how.

Shiny Server – A DIY Approach

Setting up Shiny Server from scratch can be tricky when doing it the first time. There’s an open-source version available, but it implies a from-scratch setup and configuration on a remote server.

Yes, you’ll need a server. We’ve chosen the free EC2 Ubuntu instance on Amazon AWS. From this point, we assume you’ve provisioned a computing instance and connected to it over SSH:

Image 6 - EC2 AWS instance

Image 6 – EC2 AWS instance

Once inside, there are a couple of commands you’ll have to run:

Running these will take a couple of minutes at least, but once done you’ll see that the Shiny Server service is running:

Image 7 - Shiny Server status on an EC2 instance

Image 7 – Shiny Server status on an EC2 instance

You can examine the server configuration files through the Terminal or by connecting via a text editor, such as Visual Studio Code. The file under /etc/shiny-server/shiny-server.conf tells us everything we need to deploy the app:

Image 8 - Shiny server configuration file

Image 8 – Shiny server configuration file

The Shiny Server is located under /src/shiny-server, and it’s running on port 3838. That’s a problem since only port 22 is accessible from anywhere.

You’ll need to change the inbound rules of the EC2 instance before proceeding. Open your instance details on AWS, click on the Security tab and click on the security group. You’ll see Inbound rules at the bottom, so click on Edit inbound rules.

Add a new rule according to the second list item in the following image:

Image 9 - Adding a new inbound rule for port 3838

Image 9 – Adding a new inbound rule for port 3838

That’s all you have to do configuration-wise. Now copy your local Shiny app to /srv/shiny-server/<app-folder-name>. We’ve named our folder myshinyapp and it contains a single app.R file.

Interested in R Shiny? Make it your career in 2022 by following our detailed guide.

You can now finally open your Shiny app. The URL structure is as follows: <instance_ipv4>:3838/<app-folder-name>:

Image 10 - R Shiny app running on an EC2 instance

Image 10 – R Shiny app running on an EC2 instance

And there you have it – the app is successfully running on a remote computing instance. It took us a while to configure the server, but it’s now running completely free of charge on a free EC2 instance.


Conclusion

Sharing R Shiny applications can be a daunting task for beginners. Sure, you can upload a gist to GitHub, but that’s not enough if you want to share the app with non-technical users. We’ve explored two alternatives – one free (to a degree) with almost no setup and the other that’s also free but requires you to set up a cloud compute instance and configure everything yourself.

You can’t go wrong with either. The latter offers the most customizability but is also the most difficult to manage.

Now if you’re sharing a Shiny app for enterprise purposes, you’ll want something a little more robust. If you need assistance here, Appsilon is an RStudio Full Service Certified Partner. We develop advanced R Shiny applications for Fortune 500 companies across the globe. We’d be happy to help you choose the right options for your use case.

What is your preferred way to share R Shiny apps? Please let us know in the comment section below.

Article How to Share R Shiny Apps – Top 3 Methods Explained comes from Appsilon | Enterprise R Shiny Dashboards.

To leave a comment for the author, please follow the link and comment on their blog: r – Appsilon | Enterprise R Shiny Dashboards.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)