Thursday, June 15, 2017

Ramazon: Get your shiny app on amazon web services

So now you've written a couple of Shiny apps and want to share them with friends, family, colleagues, pets. You'll need to host it somewhere. One solid option is hosting it for free on shinyapps.io. The downside to this is that their free tier only allows for 25 hours of CPU time per month. For many this is more than sufficient but if you have several apps that get used frequently you can hit this cap quickly. A second option is hosting the app yourself. This can be done on any machine that you can install shiny server onto.

Amazon, has precisely such machines, (one is currently running the F1 plotting app I wrote in a previous post) and the best part is they're free to use (for one year...). Setting up an amazon EC2 web instance is pretty straight forward but installing R, shinyserver and configuring the app can be a little tricky especially if you aren't comfortable in a linux environment. That's where the package Ramazon comes in.

Ramazon is a packaged developed by Andrea Cirillo which I recently helped in updating. The package includes two functions: ramazon() and ramazon_update() which get your shiny web app up and running on amazon hassle free. It works by essentially creating a bash script that will access your instance using ssh, install R and shiny server, install the app packages, and upload your app files using scp.

Here's how to get going:

First we need to set up an Amazon EC2 instance. To do that we need to create an account on their free tier which is good for one year.

Next we set up an EC2 micro instance. This tutorial from the original Ramazon developer Andrea Cirillo will get us there:


How to launch an aws ec2 instance from Andrea Cirillo 

After following the above steps, we should have a running EC2 micro instance. We should also have our keypair file downloaded, and our public IP address for the instance ready. The next step is to move the keypair.pem file to the shiny app folder since that's where Ramazon expects to find it.

Now we're ready to deploy our app! Note that I recommend restarting R so that you only install the necessary packages onto your EC2 instance.

#get Ramazon
install.packages("devtools")
library(devtools)
install_github("andreacirilloac/ramazon")
# set working directory to the app directory
setwd("~/your_app")
# Run Ramazon
ramazon(Public_DNS, key_pair_name,test = FALSE) 

Your Public_DNS will look like: ec2-XX-XXX-XXX-XX.us-west-2.compute.amazonaws.com
Your key pair will look like:  key_pair (drop the .pem)

Ramazon will output a lot of code salad to the terminal and will create a file called bash_script.txt in your working directory. This script is useful for debugging so save it for now!

That should be it! Check out:
ec2-XX-XXX-XXX-XX.us-west-2.compute.amazonaws.com:3838/your_app

And...?

It worked!

Great! Share your app!
If the hideously long url turns you off you can configure an existing domain/subdomain you own to point to it. To do that you simply create a DNS A record with the EC2 IP address. Then your app will be available at example.com:3838/your_app. You can get this down to example.com but you need to reconfigure the shiny server to listen on port 80 and forward traffic to the app folder.

It didn't work =(

The first thing to check is what exactly didn't work. If when you navigate to the page and see something like this:


There was a problem with the shiny server install. If you're comfortable with Bash scripts you can look at the bash_script.txt file to make sure everything looks ok. If you can't figure that out contact Andrea Cirillo on github or me and we'll sort it out.

If you see something like this:
ERROR: An error has occurred. Check your logs or contact the app author for clarification.
It means shiny server was installed but there was a problem with the app. The most likely cause of this is that a necessary package didn't get installed properly. You can figure out what went wrong by adding options(shiny.sanitize.errors = FALSE) to the top line of your server.R and ui.R files.

Finally it's worth noting that your authorized IP address that you use to ssh and scp into the web instance will need updating if you try and run Ramazon on a later date or from a different computer. You can update this setting using the AWS EC2 instance control panel.

Good luck! 

No comments:

Post a Comment

Pokemods! An educational outreach initiative

Pokemodels! An educational outreach initiative This post originally appeared on The Node . Getting the next generation...