How to put a Shiny app into production

If you use R, then surely you know Shiny, one of the R packages, which allows you to create web applications completely in R. Shiny is very powerful and it is very easy to put it into production if we use Shinyapps, but … what if we want to put our Shiny app into production without relying on anyone else?

In this post, I explain how you can put a Shiny app into production using two different Google Cloud services: Cloud Run and Compute Engine. For this, we will use Docker, the Google Cloud SDK, and Google Cloud itself.

In any case, why should you learn to upload your Shiny app to different Cloud services if you already have Shinyapps? I’ll explain it to you.

Why upload a shiny app to Google Cloud

When we put a Shiny app into production we have two options: use Shinyapps or have to upload it ourselves to a Cloud environment.

Generally, for simplicity, we use the first case since Shinyapps offers to host apps for free. However, it has several limitations:

  • Maximum you can only upload 5 applications.
  • All applications can only run for 25 hours. If people use it more then they will stop working. And yes, 25 hours is not as long as it seems.
  • In terms of storage, the application will have a maximum of 1Gb of space in the Free version. The more capacity you need, the more you will pay, being the maximum 5Gb of application.
  • Regarding processing, the application will have a maximum of 1Gb in the Free version.

In short, if you don’t think you’re going to exceed any of these limitations, go ahead, Shinyapps is the option to choose. However, with medium-sized applications, it is very possible that you will overcome it, and, in those cases, it is probably cheaper to upload the app to a Cloud environment than to upload it to Shinyapps.

Well, now that you know why you should know how to put a Shiny app into production with Google Cloud, let’s see how it’s done.

How to put a Shiny app into production on Google Cloud

Step 1. Create the Docker image

Docker is a software widely used in Data Science to isolate and package our software so that we can run it in any environment with Docker.

If you don’t know about Docker, I recommend that you read the tutorial on Docker for Data Science that I created. < / a>

In our case, to create a Docker image that uses Shiny, we will start from the rocker/shiny image, which has everything we need. As the documentation indicates, the application directory is located in /srv/shiny-server/, so that is where we will have to paste our files.

In my case I have the following structure:

├── app
│   ├── ui.R  
│   └── server.R
└── Dockerfile

Taking this into account, the Dockerfile file is the following:

FROM rocker/shiny

# Install Packages
RUN R -e 'install.packages(c("shiny"))'

# Copy App
COPY ./app/* /srv/shiny-server/

# run app
CMD ["/usr/bin/shiny-server"]

With this, we already have the Dockerfile and we can mount the Docker image:

docker build -t pi_app .

Now we can launch the Docker image:

docker run --rm -p 3838:3838 pi_app

Finally, we can check that it works correctly by accessing the localhost: 3838 page.

Shiny app running locally

Great, we already have our application running locally with Docker, now let’s see how to put our shiny app into production, for which we will have to upload this Docker image to Google Cloud.

Step 2. Upload Docker image to Google Cloud

To upload a Docker image to Google Cloud we will have to do three things:

  1. Enable the required APIs.
  2. Install and configure the Google Cloud SDK.
  3. Upload the image to the Container Registry.

Let’s go through these steps one by one.

1. Enable the required APIs

The first of all to upload a Docker image to Google Cloud is to enable the necessary APIs in Google Cloud. More specifically we have to enable the APIs of Cloud Run, Cloud Build, and Container Registry.

Although this is something that we have already seen in other posts such as putting an R model in production or automating R scripts in Google Cloud, I explain how to do it:

  1. Go to the Google Cloud API library (link) and look for the API that you want to activate.
  2. Enable the API by clicking the enable button.

2. Install and configure the Google Cloud SDK

An SDK (Software Development Kit) allows us to interact with another service in a simple way. In this case, we can work with Google Cloud from our own computer terminal. In this way, for example, we can upload a Docker image to the Container Registry, among many other things.

To download the Google Cloud SDK you simply have to access this link, choose your operating system and download it of the link that will appear in the explanatory sheet.

Once you have installed it, you must configure it. More specifically, you must do three things:

  1. Update the Cloud SDK . It is not required, but it is usually a good idea. To do this you must execute the following command:
gcloud components update

2. Link your computer with your Google Cloud account. To link you will simply have to execute the following command in the console:

gcloud auth login

3. Link Docker with Google Cloud. To do this, you will first need to enable Docker, which you can do by running the following command in the console. After you have done this, you can connect Google Cloud and Docker with the following command:

gcloud auth configure-docker

Now that we have configured the Cloud SDK, let’s see how to upload the Docker image to the Container Registry.

3. Upload Docker Image to Container Registry

To upload a Docker image to the Container Registry we must have the following information: & lt; / p & gt;

1. Name of the image. If you remember, we gave this name when mounting the image using the Dockerfile using the command:

docker build -t <nombre_imagen>.

2. Google Cloud account ID: You can find it in the project table of your Google Cloud account.

How to put Shiny app in production

Once we have the previous information, we will have to give a new name to our Docker image. To do this, open a console in the terminal and run the following command:

docker tag <nombre imagen>< project id>/<nombre imagen>

# My case
# docker tag app_pi

Once we have given it a new name, we can upload it to the Container Registry. To do this, you just have to execute the following command:

docker push< project id>/<nombre imagen>

# In my case
# docker push
How to put Shiny app in production

If you have followed the steps and you have not skipped any errors (and you get something like the previous image) perfect, you already have your app uploaded in the Container Registry. Now we will only have to deploy it in one of the different services offered by Google Cloud.

Step 3. Deploy the Docker image to a Google Cloud service

Once we have chosen the Docker image, the moment of truth comes: choose the Google Cloud service in which we will deploy our application.

Deploying our Shiny app on Google Cloud is very simple. First, we go to the Container Registry, we choose the bucket that has the same name as our image and we choose the image. Once done, we will get a screen like the following:

Shiny App deployment in Google Cloud

At the top, there is a button with the text “Deploy”. Clicking will give us three options:

  • Cloud Run: it is a serverless service, that is, you do not have a contracted server, but servers are being raised as requests are received. The good thing is that, if you don’t have traffic to the page, you won’t pay.
  • Google Compute Engine (GCE): in this case you do hire a server, so all requests will be returned by the server.
  • Google Kubernetes Engine (GKE): It is similar to Cloud Run, with the difference that you can modify every aspect of orchestration, storage, etc. Personally, in the case of a Shiny application I don’t think it makes much sense.

Also, in both Cloud Run and Compute Engine you can choose the server capacities, both the number of CPUs and the amount of RAM. In both cases, it is a dropdown.

So how do you choose between deploying the Shiny app on Cloud Run or Compute Engine?

How to choose between Cloud Run and Compute Engine

Well, although a Cloud Run server could run several applications at the same time, the reality is that Shiny is a stateful service, while Cloud Run is designed for stateless services.

This means that you will only be able to have a single application per server and Cloud Run only offers to have 1000 servers up at the same time.

Therefore, if you assume that your Shiny application will have more than 1000 concurrent users, then you can use Cloud Run, otherwise, you will have to use Compute Engine.

Anyway, this will not be a problem in the vast majority of applications Shiny will not exceed this limit. And if they do, having the application on Docker would be as easy as deploying on Compute Engine.

In any case, when deploying the application in Cloud Run, the number of requests per container will have to be set to one, as in the following image:

How to put Shiny app in production

Also, remember to set the Cloud Run connection port the same that you set in Docker (in my case 3838).

If you do this correctly, you will have deployed the application and a summary screen like the following one will appear:

Shiny App deployment in Google Cloud

If this is your case, congratulations! You already have your Shiny application in production. To access it, you will simply have to visit the URL that appears on the top page of the screen:

Cloud Run Price

Perfect, you’ve already put your application into production, but … how much is it going to cost? Well, it depends on the use it may even be free. And it is that, by default, Cloud Run for free offers:

  • 50 hours of CPU.
  • 100 hours of use of 1Gb RAM (if you use 2Gb / machine then it would be 50).
  • 2 million requests.

Come on, if you are going to use an application that has 1CPU and 1Gb of RAM, while with Shinyapps you would have only 25 free hours, with Cloud Run you will have 50. Also, for another 50 you should only pay for the CPU, which costs $ 0.00002400 / s (link). That is, the first 100 hours of your application will only cost you $ 4.32, while in Shinyapps it costs more than double ($ 9 / month).

In short, putting your Shiny app into production in the Cloud is more laborious than uploading it to Shinyapps, but, undoubtedly, at the cost level, it is worth it.


As you can see, if you know about Docker it is very easy to put your Shiny applications in production in Cloud services. This will give you great flexibility and scalability when it comes to making your applications reach hundreds of people.

I hope you liked the post. See you in the next one!