Launch Shiny within Docker
Tutorial contributed by Drew Schmidt. Note: User-provided tutorials are not supported by CADES.
Shiny is a web app framework for the statistical programming language R. And while that might sound crazy, it actually works very well!
Shiny is generally pretty easy to develop locally, but deploying it in Shiny Server can be a bit of a headache if you go about it the wrong way. This guide should help make the process as painless as possible.
For simplicity, we will be using Shiny inside of a Docker container. Since shiny is a web app, that might lead you to think this process is going to be considerably more complicated than running it natively. However, I can assure you that this is not the case, particularly since the Rocker Project has handled most of the configuration details for us. They provide a shiny container already set up for business. However, if you wish to install Shiny server natively for some reason, then you will find some useful details in their Dockerfile.
So the first step is to set up an Ubuntu 16.04 vm on the CADES Cloud with Docker configured. See the Docker tutorial to learn how to set up Docker.
Since Shiny is a web app, we need to open some ports on the vm so that we can access it from a laptop. We also have to do something similar for the Docker container so it can communicate with the vm, but that is actually much easier (and explained in the next section).
We will be using port 80. If you need to open another port, you can, but it's not as simple because port 80 is a pre-configured rule set for you. The Run a Simple Web Server guide from the official CADES Cloud documentation is quite helpful here.
The condensed version is you want to navigate to Access & Security and:
- click Create Security Group and give it a name (I called mine
- click Manage Rules
- click Add Rule
- select http (this will open port 80; if you want another port, set up a custom tcp rule)
- navigate to your instance (under the Instances tab)
- click the triangle button next to Create Snapshot, select Edit Security Groups
- add your new security group by clicking the blue "plus" button
That may sound like a lot or feel a bit overwhelming, but it's not so bad. Give it a try!
Run an Existing Shiny Container
Example 1: k-means Demo
Ok, we're finally ready to start talking Shiny. Assuming you've got your vm with Docker running, we'll first run some example (already configured and built) apps.
sudo docker pull wrathematics/shinykmeans sudo docker run -i -t -p 80:3838 wrathematics/shinykmeans
If you chose a port other than 80 when setting up the security groups, then you will need to change the
80 above accordingly.
Now just point your web browser to the IP of your vm, which you can find from the Compute -> Instance tab on the Birthright Cloud dashboard. Example screenshot below (with actual IP addresses masked)
So if your ip is
184.108.40.206, then you just need to go to
http://220.127.116.11. If you chose to use a port other than 80, then you will need to append a colon
: and that port to the end of your ip address. So if you chose port
5555, then you would go to
Example 2: Plot Builder
Something that shows off the power of R and Shiny a bit more is the app "ggplotwithyourdata". For your convenience, this too has been set up on Docker Hub, and is similarly easy to use:
sudo docker pull wrathematics/ggplotwithyourdata-docker sudo docker run -i -t -p 80:3838 wrathematics/ggplotwithyourdata-docker
As before, just point your web browser to your vm's url and you're good to go.
Deploying Your Own Shiny App
Fortunately or unfortunately (depending on your perspective), this is the most tricky part. In truth, this goes a bit beyond the scope of this document. Only you really know the dependencies of your app, so we can't tell you exactly what to do.
But we can give some general advice:
- You will need to create your own Dockerfile. This guide is also not the right place to learn all about the various options of Dockerfiles; this is. However, generally speaking, a Dockerfile is not that far removed from a shell script. So knowing how to build the app natively is important (and really, no different) in understanding how to get it to build in the container.
- See how things are done on existing Dockerfile configurations. The k-means configuration is available here and the plot one here. These have been deliberately kept fairly simple.
- Installing R packages from source is often much harder and always much more time consuming than installing binary packages. When you run
devtools::install_github()or the like, you are installing a source package. However, there are many binary packages available in
apt. These packages all have the prefix
r-cran-, so you should check what is available before going the
install.packages()route. However, sometimes the
r-cran-packages are out of date, so check before you install.
- You can run multiple apps in the same Docker container. You just place each one in its own subdirectory of
/srv/shiny-server. So if you have apps
barthat you want to host in the same container (say they have very similar dependencies), you would put them in
/srv/shiny-server/bar. Then you would view them at
http://18.104.22.168/bar(replace the fake ip with your real one of course!).
- For troubleshooting Shiny Server problems, see the Shiny Server Open Source Administrator's Guide.