Guide to Shiny apps with Shiny Server on Amazon EC2
Preface: posting this for archive purposes only. This was the first of its kind and has been succeeded by better guides.
I am writing this guide because this guide did not exist when I decided to put my 2010 US Census Shiny App on Amazon’s servers. Surely I can’t be the only one who’s never had any experience with EC2 (or SSH or vi, for that matter).
So here’s a newbie’s guide to newbies for deploying your rad Shiny app on Amazon Elastic Compute Cloud (EC2) from scratch. It took me sixteen 30 Rock episodes to figure this stuff out (counting the time it took to download the census data) but hopefully you’ll have your app up and running in less time than…a BBC Sherlock episode.
What are Shiny and Shiny Server?
Shiny is an R package developed by the incredible folks at RStudio for making interactive web applications. Shiny Server is a server program that makes Shiny applications available over the web.
Amazon Elastic Compute Cloud (EC2)
Amazon EC2 is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale computing easier for developers. If you have an Amazon account then you can start using Amazon Web Services (AWS) for free! AWS Free Tier includes 750 hours of Linux or Windows Micro Instances each month for one year.
Setting up Shiny web apps on Amazon EC2
Launching an EC2 Instance
The process for creating and launching a new EC2 instance is pretty straightforward. I’d recommend going with Ubuntu 11.10 and then carefully thinking about how much space you’ll be using. For example, my app uses the 2010 US Census data which comes in several R packages that total 4.7 GB. You’ll have to create a security key and download it. Remember where it gets downloaded to as you’ll have to use it to connect with the instance through SSH.
If you’re on Linux or OS X, you can use Terminal and run all the commands from there. On Windows you have to download and install PuTTY. I’m writing this on a Mac, my apologies if you run into a problem on Windows.
cd Downloads ssh -i that_key_you_downloaded_earlier.pem email@example.com
sudo apt-get update sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs npm
Installing R and packages
Before you start installing R and Shiny, you need to add a source so that when you install R the latest version (2.15.2) gets installed. If you skip this step then you’ll end up installing 2.12 and nothing will work.
Usually you’d open the sources list in gedit or another text editor which has an interface. In this case we’ll have to use vi to add our R source. I haven’t used vi until today and found this cheat sheet invaluable for learning it.
sudo vi /etc/apt/sources.list.d # go <span class="keyword">in there and there should be a list file there from the Node.js step # open that list file <span class="keyword">for editing # type <span class="keyword">in: o # you will then be able to type text on a new line # type <span class="keyword">in: deb http://lib.stat.cmu.edu/R/CRAN/bin/linux/ubuntu/ version/ # where version=oneiric or precise or whatever # make sure to have a space there! Otherwise you'll the Malformed Line error. # You can use other CRAN repos; you're not limited to CMU. # [ESC] to finish editing. To exit and save changes: :x
Once you’re done with that, it’s time to install R. Just run the following code (thanks to Ananda Mahto from SO:
gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9 gpg -a --export E084DAB9 | sudo apt-key add - sudo apt-get update sudo apt-get install r-base
Then you’ll install the Shiny package and Shiny Server itself:
sudo su - -c <span class="string">"R -e \"<span class="functioncall">install.packages(<span class="string">'shiny', repos=<span class="string">'http://cran.rstudio.com/')\<span class="string">"" sudo npm install -g shiny-server
# Create a system account to run Shiny apps sudo useradd -r shiny # Create a root directory <span class="keyword">for your website sudo mkdir -p /var/shiny-server/www # Create a directory <span class="keyword">for application logs sudo mkdir -p /var/shiny-server/log # To be able to run shiny-server as a process later on: wget https://raw.github.com/rstudio/shiny-server/master/config/upstart/shiny-server.conf sudo cp shiny-server.conf /etc/init/shiny-server.conf
ui.R and server.R
We need to download ui.R and server.R (and any auxiliary files). This is done with wget:
sudo apt-get install wget wget https://raw.github.com/.../master/myapp/server.R wget https://raw.github.com/.../master/myapp/ui.R mkdir myapp mv ui.R myapp/ui.R mv server.R myapp/server.R sudo cp -R ~/myapp /var/shiny-server/www/
Run sudo start shiny-server to start and sudo stop shiny-server to stop. Open your web browser and go to
Optional: Elastic IPs and Dyn DNS
Problem: You own a domain name and DNS hosting from a service like Dyn.com and want people to use your Shiny app by going to your domain.
Solution: In the AWS Management Console go to EC2 and then Elastic IPs. You can allocate a new address and then associate it with an instance. You can then use that IP when you make a new hostname the Dyn control panel.
You got yourself a Shiny app running on Shiny Server on Amazon EC2. Remember to watch out for those free tier limits!