Site icon R-bloggers

HOW TO HOST YOUR SHINY APP ON AMAZON EC2 FOR MAC OSX

[This article was first published on numbr crunch - Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In a previous post I had mentioned that I would provide a hosting guide for Shiny applications so other people wouldn’t have to suffer through all the same issues I did when trying to do this. Just for reference, I am running OSX Yosemite. For the SSH client you’ll be using Terminal, for the SFTP client you’ll be using Cyberduck. Much of this guide was put together with information from here and here.

Setting up an Amazon EC2 Instance
Go to aws.amazon.com.

Create an account if you don’t already have one. You can use your regular Amazon account to login as well.

In the Amazon Web Services page, go to EC2, under Compute.

You should now be at the EC2 dashboard. Click the blue button “Launch Instance” button.

For the AMI selection, select Ubuntu Server 14.04, it’s a 64-bit server that is free tier eligible.

Continue clicking next through the configuration options until you reach Step 6: Configure Security Group. Input the security settings as seen in the below image. The initial SSH setting makes sure only you can access your Amazon EC2 instance to make changes. The HTTP setting allows anyone to access your web application. The last setting connection is for Shiny server specifically. Once you have input all the correct settings, click “Review and Launch”. *Just a note that these are suggested options for security, please use discretion and read Amazon EC2 security documentation for additional details.
You’ll get a warning about security on the next page, just click the blue “Launch” button.

If you don’t already have an AWS keypair (from a previous instance), create a new key pair and give it a name. Download it and remember where you saved it. Then click the blue “Launch Instances” button.

Click “View Instances”, your instance should be starting up right now. Click on the instance to view some useful information (Public DNS).
Connect to Server via SSH
Open up Terminal in your Mac, type the following command.

< color="#8d2424">ssh -i [path to your AWS keypair .pem file] ubuntu@[your public DNS < >< color="#8d2424">address]< >

example:

< color="#8d2424">ssh -i /Users/vabraham24/Documents/RStudio/Income_Race/AWS_keypair.pem ubuntu@< >< color="#8d2424">ec2-54-151-55-157.us-west-2.compute.amazonaws.com< >

Type ‘yes’ if prompted about whether you’re sure you want to connect, and press enter. 

The command line prompt will change to something like the following: < color="#8d2424">ubuntu@ec2-54-151-55-157:~$< >< color="#8d2424"> < >

Install R (from http://www.rstudio.com/products/shiny/download-server/)
Run the following in Terminal.

< color="#8d2424">sudo apt-get update
sudo apt-get install r-base
sudo apt-get install r-base-dev< >

Install the Shiny package and other packages as needed (from http://www.rstudio.com/products/shiny/download-server/)
Run the following in Terminal.

< color="#8d2424">sudo su –
-c “R -e “install.packages(‘shiny’, repos=’http://cran.rstudio.com/’)””< >

If you have other packages you need to install for you application, copy the above line and replace ‘shiny’ with your respective package name.

Install Shiny Server (from http://www.rstudio.com/products/shiny/download-server/)
Run the following in Terminal.

< color="#8d2424">sudo apt-get install gdebi-core
wget http://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.3.0.403-amd64.deb
sudo gdebi shiny-server-1.3.0.403-amd64.deb< >

Install an SFTP client (Cyberduck) and upload your Shiny application files
I find interfaces for transferring files more intuitive than the command line, for this task we are going to be using Cyberduck (you can also download this from the App Store). Once Cyberduck is open, click “Open Connection”. Select SFTP (SSH File Transfer Protocol). In the server field, copy and paste the Public DNS from the Amazon EC2 instance. For Username, input “ubuntu”. Under More Options, click the “Use Public Key Authentication” checkbox and direct it to you AWS keypair from earlier. Click “Connect”.
You should now be in the directory /home/ubuntu.

Since you have installed Shiny and Shiny Server, the following directory should have been created /srv/shiny-server, this is where all your application files will go. Unfortunately Cyberduck doesn’t have permissions in all of the directories within the server. In order for you to upload your files to this directory, you will have go back to the Terminal and type the following.

< color="#8d2424">sudo chown -R [Cyberduck username] [directory where you want write permissions]< >

example:

< color="#8d2424">sudo chown -R ubuntu /srv/*< >

There should already be example files within the /srv/shiny-server directory. You can now delete those and drag your Shiny application files from your local drive directly to this location. Be sure to update relative paths for any reference files you are using.

View your hosted Shiny application
Once all your Shiny files are loaded on the server, use the following url format to view your app!

< color="#8d2424">[public DNS]:3838< >

example:

< color="#8d2424">ec2-54-151-55-157.us-west-2.compute.amazonaws.com:3838< >

Congrats, you just hosted your first Shiny app!

Hosting multiple applications on a single server
If you want to host multiple applications, you can do that by creating separate directories for each app. For example, create the folder “Income_Race” in the /srv/shiny-server directory. Then place the respective ui, server, and reference files in that directory (/srv/shiny-server/Income_Race). Host additional applications by adding new folders under the /srv/shiny-server directory. You can view the app with the following url format.

< color="#8d2424">[public DNS]:3838[new_directory]< >

example:

< color="#8d2424">ec2-54-151-55-157.us-west-2.compute.amazonaws.com:3838/Income_Race< >

To leave a comment for the author, please follow the link and comment on their blog: numbr crunch - Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.