How to Share R Shiny Apps - Top 3 Methods Explained

By:
Dario Radečić
February 22, 2022

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.
<blockquote>Interested in R beyond Shiny? <a href="https://appsilon.com/r-markdown-powerpoint-presentation/" target="_blank" rel="noopener noreferrer">Learn to make PowerPoint presentations with R Markdown</a>.</blockquote>
Table of contents:
<ul><li><a href="#basic-shiny-app">Let’s Code a Basic R Shiny App</a></li><li><a href="#github">How to Share R Shiny Apps on GitHub</a></li><li><a href="#shinyappsio">ShinyApps.io - Share your R Shiny Apps for Free</a></li><li><a href="#shiny-server">Shiny Server - If You Want To Do It Yourself</a></li><li><a href="#conclusion">Conclusion</a></li></ul>

<hr />

<h2 id="basic-shiny-app">Let’s Code a Basic R Shiny App</h2>
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 <em>hello</em> message. Think of it as a “Hello World” for R Shiny.

Open <a href="https://www.rstudio.com/products/rstudio/download/" target="_blank" rel="noopener noreferrer">RStudio</a> and create a new file called app.R. Then paste the following code:

<script src="https://gist.github.com/darioappsilon/91e8a3e518c8cbc8ffbdb68af59b191a.js"></script>

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

<img class="size-full wp-image-12155" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ecb27d78ec82883aa2b_Hello-World-R-Shiny-app.gif" alt="Image 1 - “Hello World” R Shiny app" width="906" height="712" /> 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 <a href="https://templates.appsilon.com/" target="_blank" rel="noopener noreferrer">Appsilon's Shiny Templates</a> for free. Then you can customize your new Shiny templates in less than 10 minutes with our <a href="https://appsilon.com/r-shiny-dashboard-templates/" target="_blank" rel="noopener noreferrer">guide on developing Shiny dashboards</a>.
<h2 id="github">How to Share R Shiny Apps on GitHub</h2>
You have two options if you want to share R Shiny apps through GitHub:
<ol><li><strong>As a repository</strong> - 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.</li><li><strong>As a gist</strong> - Only for a single-file R Shiny apps, like ours is. You can load the Shiny application directory from a GitHub gist in R.</li></ol>
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 (<code>app.R</code>), so let’s proceed with gists. Log in to your GitHub account and select a <em>New Gist</em> option under the plus sign in the top right corner. Add the name and code to the gist:

<img class="size-full wp-image-12147" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ecc38e39fcee754dc7e_Creating-a-new-GitHub-gist.webp" alt="Image 2 - Creating a new GitHub gist" width="2122" height="1226" /> 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: <code>gist.github.com/&lt;your_username&gt;/&lt;gist_id&gt;</code>. The last part (<code>&lt;gist_id&gt;</code>) is all we need.

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

<script src="https://gist.github.com/darioappsilon/9593937767736911ea36aae3411184d8.js"></script>

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

<img class="size-full wp-image-12159" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ecec37dda6258e5b00f_Running-an-R-Shiny-app-from-a-GitHub-gist.webp" alt="Image 3 - Running an R Shiny app from a GitHub gist" width="3012" height="1856" /> 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.
<h2 id="shinyappsio">ShinyApps.io - Share your R Shiny Apps for Free</h2>
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 <a href="https://www.shinyapps.io/" target="_blank" rel="noopener noreferrer">$9 per month</a>.

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 <code>rsconnect</code> package from the R console and authenticate through it. The authentication details (name, token, and secret key) are listed on the shinyapps dashboard page:

<script src="https://gist.github.com/darioappsilon/b1e5f24dea20a42b89fe557b992111d0.js"></script>

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

<script src="https://gist.github.com/darioappsilon/d4fc32f84258d8c77a1ed5b4c2f5f915.js"></script>

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

<img class="size-full wp-image-12151" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ecefdb8ad62e9cde09b_Deploying-a-Shiny-app-to-ShinyApps.io_.webp" alt="Image 4 - Deploying a Shiny app to ShinyApps.io" width="1518" height="582" /> 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:

<img class="size-full wp-image-12149" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ecfa89a044c4d35a870_Deployed-R-Shiny-app-on-ShinyApps.io_.webp" alt="Image 5 - Deployed R Shiny app on ShinyApps.io" width="2394" height="1932" /> 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.
<h2 id="shiny-server">Shiny Server - A DIY Approach</h2>
Setting up <a href="https://www.rstudio.com/products/shiny/download-server/" target="_blank" rel="noopener noreferrer">Shiny Server</a> 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 <a href="https://aws.amazon.com/ec2/?nc2=h_ql_prod_fs_ec2" target="_blank" rel="noopener noreferrer">EC2 Ubuntu instance</a> on Amazon AWS. From this point, we assume you’ve provisioned a computing instance and connected to it over SSH:

<img class="size-full wp-image-12153" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ed4369f5360b2ad7a0f_EC2-AWS-instance.webp" alt="Image 6 - EC2 AWS instance" width="2770" height="1450" /> Image 6 - EC2 AWS instance

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

<script src="https://gist.github.com/darioappsilon/39b0ac633f99591e986de94552b57a8b.js"></script>

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

<img class="size-full wp-image-12163" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ed42d3baed3c5a8db9d_Shiny-Server-status-on-an-EC2-instance.webp" alt="Image 7 - Shiny Server status on an EC2 instance" width="2770" height="1450" /> 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 <code>/etc/shiny-server/shiny-server.conf</code> tells us everything we need to deploy the app:

<img class="size-full wp-image-12161" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ed64f1c9b360c7e68c2_Shiny-server-configuration-file.webp" alt="Image 8 - Shiny server configuration file" width="2662" height="2018" /> Image 8 - Shiny server configuration file

The Shiny Server is located under <code>/src/shiny-server</code>, 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 <em>Security</em> tab and click on the security group. You’ll see <em>Inbound rules</em> at the bottom, so click on <em>Edit inbound rules</em>.

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

<img class="size-full wp-image-12145" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ed74f1c9b360c7e69a6_Adding-a-new-inbound-rule-for-port-3838.webp" alt="Image 9 - Adding a new inbound rule for port 3838" width="2692" height="1904" /> 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 <code>/srv/shiny-server/&lt;app-folder-name&gt;</code>. We’ve named our folder <code>myshinyapp</code> and it contains a single <code>app.R</code> file.
<blockquote>Interested in R Shiny? <a href="https://appsilon.com/how-to-start-a-career-as-an-r-shiny-developer/" target="_blank" rel="noopener noreferrer">Make it your career in 2022 by following our detailed guide</a>.</blockquote>
You can now finally open your Shiny app. The URL structure is as follows: <code>&lt;instance_ipv4&gt;:3838/&lt;app-folder-name&gt;</code>:

<img class="size-full wp-image-12157" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ed992a26c843b62dfb4_R-Shiny-app-running-on-an-EC2-instance.webp" alt="Image 10 - R Shiny app running on an EC2 instance" width="2554" height="2000" /> 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.

<hr />

<h2 id="conclusion">Conclusion</h2>
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, <a href="https://appsilon.com/" target="_blank" rel="noopener noreferrer">Appsilon</a> is an <a href="https://appsilon.com/appsilon-is-an-rstudio-full-service-certified-partner/" target="_blank" rel="noopener noreferrer">RStudio Full Service Certified Partner</a>. 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.

<em>What is your preferred way to share R Shiny apps?</em> Please let us know in our Shiny community.

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!

Sign up for ShinyWeekly

Join 4,2k explorers and get the Shiny Weekly Newsletter into your mailbox
for the latest in R/Shiny and Data Science.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
r
shiny
deploy shiny apps
shiny dashboards