Thomas Goossens

Geographer/Data-analyst/Coder

stackoverflow spotify github linkedin email
Building a dockerized shiny server with packaged shiny apps and deploy it publicly in 2 minutes
Aug 23, 2018
4 minutes read

Let’s imagine you want to build an interactive data vizualization app that could be accessible from the internet. As an R expert, one of your best bet could be to use the power of shiny apps. You probably wonder what could be one of the most efficient way to easily and quickly deploy it ? Let’s find out how the magic of docker and R packages will help you achieve this !

Write your shiny App as a package

Building shiny apps as package will help you to produce a cleaner and easier to maintain code thanks to devtools and testthat. I heavily recommand you to code your app this way. Doing so will also allow you to use devtools::install_github() to quickly install your app on any computer, server and Docker container.

Moreover R packages are the most efficient way to make reproductible science.

To understand how to code your shiny app as a package, Dean Attali’s answers in this stackoveflow thread, and his blog post “Supplementing your R package with a Shiny app” are excellent starting points.

Follow the instructions and make sure that your shiny app code is placed into the inst folder of your package.

Create a shiny server docker image which contains the packaged shiny app(s)

A shiny server allows you to distribute multiple shiny apps over the internet at their own URL in an easy and efficient way. Setting up a shiny server may be time consuming but thanks to Docker and the rocker-org community, you can now quickly set up your own shiny server ! To do so, simply clone the rocker-org shiny server repo :

$ git clone git@github.com:rocker-org/shiny.git

Open the Dockerfile (which is the recipe to create the Docker image that will be used to instantiate the shiny server) and add the instruction line to add your packaged shinyApp to your Docker image. If your packaged app is pushed to a github repository, paste this line in the Dockerfile and adapt the <PLACEHOLDERS> :

RUN R -e "devtools::install_github('<GITHUB_USERNAME>/<PACKAGE_REPO_NAME>', ref='<BRANCHNAME>', force=TRUE)"

Repeat this step for every packaged shiny app you want to be accessible through your dockerized shiny server. Save your work and push it to github.

Make your docker shiny server image accessible through docker hub

This step will later allows you to quickly install and instantiate the shiny server docker container. To do so, simply link your github and dockerhub accounts by following the instructions provided in this post

Install the docker shiny server image on your server

Once docker hub shows that the image build is ready with no errors, download the image on your host server using the following command :

$ sudo docker pull <DOCKERHUB_USERNAME/IMAGENAME>

At this point, you only need some extra lines of R code to actually deploy your packaged shiny apps ! As your shiny apps source files are located into the inst folder of your packages, you will use the sytem.file function to actually call and initiate these. On your host server, create a folder and name it for example shiny_launchers_mountpoint. Inside this folder, create a folder for each of the shiny apps you have add to your Docker image, and give these folders the same name as your packaged shiny apps repos. Finally, inside each of this folder create and app.R file that contains the following lines (of course, don’t forget to adapt the <PLACEHOLDERS>) :

dir <- system.file("<SHINY_APP_CODE_LOCATION_INSIDE_INST_FOLDER>", package = "<PACKAGE_REPO_NAME>")
setwd(dir)
shiny::shinyAppDir(".")

At the root of the shiny_launchers_mountpoint create an index.html file that will act as the homepage of your shiny server. You can put whatver you want into it. A good idea is to create a link for each of your shiny apps.

Instantiate your docker container

Now that all of your infrastructure is ready, the last step is to run your shiny server. In your host server terminal, run this command to instantiate your shiny apps server container in detached mode :

sudo docker run -d --rm -p 3838:3838 -v <FULL_PATH_OF_YOUR_FOLDER_CONTAINING_THE_R_SCRIPT>:/srv/shiny-server/     -v /srv/shinylog/:/var/log/shiny-server/     <DOCKERHUB_USERNAME>/<REPONAME>

Now head at localhost:3838/apps into your browser and your index.html file will be served. Click on the link of the shiny app you want to consult and you are done !

If you want to consult it from another computer, replace localhost by the domain name or IP of your host server !

Tags:

Categories:

Back to posts


comments powered by Disqus