Deploying a Shiny app
ShinyProxy uses one or more Docker images to serve the Shiny apps to end users. If you want to deploy your Shiny apps, you will therefore need to build your own Docker image for the app.
Such a Docker image will typically contain:
- an R installation with
- all R packages the Shiny app depends on (‘dependencies’) and
- a folder which contains the
server.Rfor your Shiny app.
Write a Dockerfile
Docker images are built starting from a Dockerfile. The Dockerfile starts from a preexisting image and builds up the image command by command.
In order to simplify writing Dockerfiles for your Shiny apps, Open Analytics
made a template available in the shinyproxy-template repository on
Github that shows how
an app can typically be prepared for deployment on ShinyProxy.
The app we will use in the example is named ‘euler’ and allows to compute
Euler’s number using arbitrary precision. In the repository the
server.R files live in the folder
In order to make these very precise computations the app uses the
Rmpfr package for multiple
FROM openanalytics/r-base LABEL maintainer "Tobias Verbeke <email@example.com>" # system libraries of general use RUN apt-get update && apt-get install -y \ sudo \ pandoc \ pandoc-citeproc \ libcurl4-gnutls-dev \ libcairo2-dev \ libxt-dev \ libssl-dev \ libssh2-1-dev \ libssl1.0.0 # system library dependency for the euler app RUN apt-get update && apt-get install -y \ libmpfr-dev # basic shiny functionality RUN R -e "install.packages(c('shiny', 'rmarkdown'), repos='https://cloud.r-project.org/')" # install dependencies of the euler app RUN R -e "install.packages('Rmpfr', repos='https://cloud.r-project.org/')" # copy the app to the image RUN mkdir /root/euler COPY euler /root/euler COPY Rprofile.site /usr/lib/R/etc/ EXPOSE 3838 CMD ["R", "-e", "shiny::runApp('/root/euler')"]
Let’s walk through this Dockerfile step by step:
- The first line of the Dockerfile simply indicates that the image starts from a
openanalytics/r-basewhich has Ubuntu 16.04 LTS with a recent R version. This image is available on (Docker hub)[https://hub.docker.com/r/openanalytics/r-base/].
LABELinstruction adds metadata to an image, in this case the maintainer of the Docker image
- Next, a number of additional system packages are installed from
the Ubuntu repository using
apt-get install. The ones listed are of general use, but
- for your applications additional system libraries may be required and
can be installed in this way. For clarity we have included the installation
of an extra system library in a separate block: the R package
Rmpfrrequires the system library
libmpfr-devto be available
- Then, there is a
RUNcommand to install shiny specific R packages that need to be present for Shiny to be functional at all (
rmarkdownis included for reporting purposes).
- The next step is to install any
dependencies your specific application may have - in this case the
Rmpfrpackage for multiple-precision computing.
- Once all dependencies are present, we can copy our euler app (with the
server.Rfiles) onto the image in folder
- The line that copies the
Rprofile.siteonto the image will make sure your Shiny app will run on the port expected by ShinyProxy and also ensures that one will be able to connect to the Shiny app from the outside world
EXPOSE 3838instructs Docker to expose port 3838 to the outside world (otherwise it will not be possible to connect to the Shiny application)
CMDstatement, finally, instructs how to launch the Shiny app when the container is started.
Build the Docker Image
Navigate into the directory that contains the
Dockerfile. In our example this
is the root folder of our shinyproxy-template GIT repository.
Inside this folder launch the following docker command
sudo docker build -t openanalytics/shinyproxy-template .
to build the docker image.
As discussed under Configuration, the configuration
of ShinyProxy happens in a single file
application.yml. If one wants to add
an application, it can be specified in the
specs block as follows:
specs: - id: euler display-name: Euler's number container-cmd: ["R", "-e", "shiny::runApp('/root/euler')"] container-image: openanalytics/shinyproxy-template access-groups: scientists
The meaning of the individual fields can be consulted here.
See Getting Started guide:
java -jar shinyproxy-2.5.0.jar