Building and Sharing a Shiny App
By: Alejandro Martinez
Tags: Rstudio, Data Science, Presentation Tips, Dashboard, Docker

Building and Sharing a Shiny App

How to get started as fast as possible

All the code supporting this article can be forklifted from this MatrixDS project

Photo by Stephen Dawson on Unsplash

An often ignored piece of the data science process is deploying a complete solution that can be consumed by the non-technical decision makers. RStudio Shiny is an excellent tool for building and deploying that solution. Shiny provides data scientists with the ability to build reactive web applications all in R programming language. It is also super flexible and allows you to add HTML and CSS if you want to build something more robust. As a data scientist, I want to focus my attention on building models that generate value and want to deal with the hosting and infrastructure needs as little as possible. There are several tools to help you host a Shiny app, and the most common ones are RStudio Connect, shinyapps.io, and MatrixDS. For this tutorial, we focus on MatrixDS because it is self-contained (you do not need to use any other tool to build, host, and deploy). It also provides its users with the ability to easily collaborate on the creation of a model and the application.

TL;DR: The rest of this post loosely follows the structure of this video.

Inside any MatrixDS project, we can create a Shiny tool and an RStudio tool.

If your project does not have one already, the Shiny tool immediately creates a “shiny-server” folder and loads it with an app.R file. This file is a modified version of the Shiny app template that RStudio builds when you click “New,” and then “Shiny Web App…”. The Shiny tool creates a container that mounts and deploys whatever is inside the “shiny-server” folder.

The following lines of code were added to the Shiny template to help the deployment:

.libPaths( c( .libPaths(), “/srv/.R/library”) )

This line tells the container that hosts the Shiny app to use the same libraries installed in your RStudio instance.

production <- TRUE if(production == FALSE) {

#if you using the RStudio tool

shiny_path <- “~/shiny-server/”

home_path <- “~/”

} else {

#if you are using the shiny tool

shiny_path <- “/srv/shiny-server/”

home_path <- “/srv/”

}

Here we create a variable called production. When production is set to TRUE, the app is meant to run on the Shiny container. When production is set to FALSE, the app is meant to run inside RStudio. Because each tool is in a separate container, we must use absolute paths so they can find the files or objects that we refer to in our code.

Finally, you can share your app by hitting the private/public toggle on the Shiny tool. When set to public, anyone with the URL can see the app in production, and you can easily share it with consumers or clients. A MatrixDS account is not necessary to view a public Shiny app.

If you open the RStudio tool, you immediately get to the environment where we created the Shiny app with no installation required. Here you can make any modifications, and they immediately show up on the deployed version that your customers or colleagues are using.

Test building and deploying your Shiny app on MatrixDS now