Shinyapps.io is a great free resource for hosting your first Shiny app. However, the server space and computing power is quite limited. Upgrading to the cheapest option is $9/month which is more expensive than most web hosting companies. So is there another option to be able to host your Apps without making a dent in your wallet?
There is! Shiny Server is an open source platform that allows you to host multiple Shiny applications and can be hosted on Amazon Web Services (AWS), which charges reasonably and even has a free tier (we will be using this in today’s tutorial).
Kimberly Coffey created a wonderful guide for Mac users on how to start a Shiny Server on AWS. Freddy Drennan built on the guide and showed us how to launch RStudio Server and launch an app. My guide is dedicated to Windows Users and could not have been created without the foundations set by these two amazing individuals!
The software I’ll be using for this tutorial is:
- Windows Operating System (version doesn’t really matter)
- PuTTYgen – convert AWS .pem to a .ppk for SSH’ing
- PuTTY – for SSH’ing
- AWS EC2 Ubuntu – Linux Platform (Ubuntu Server 16.04 LTS (HVM), SSD Volume Type)
- FileZilla (to transfer local files to Instance)
Amazon Web Services (AWS) Elastic Compute Cloud (EC2)
1. If you don’t already, create an account on AWS: http://aws.amazon.com
2. Go to “EC2” and click “Launch Instance”
3. The Amazon Machine Image (AMI) I chose was “Ubuntu Server 16.04 LTS (HVM), SSD Volume Type”. It’s free tier eligible. Ubuntu Server 18.04 has some current unresolved issues with Shiny Server, which is why i chose a more stable AMI. Once you pick your AMI, hit “Select”.
4. The Instance Type is the size of RAM and CPUs your machine will have. The only Free Tier option for Ubuntu Server 16.04 is the “t2.micro”. This will serve our purpose for the most part, however if you notice that your app freezes while installing packages (like Dplyr or Leaflet), you will need to temporarily upgrade the Instance Type to install those packages. Hit “Next: Configure Instance Details”
5. Don’t change anything. Hit “Next: Add Storage”
6. Don’t change anything. Hit “Next: Add Tags”
7. Don’t change anything. Hit “Next: Configure Security Group”
8. Click “Add Rule”, put 3838 in the “Port Range” with the Source “Anywhere”. This is the Shiny Server port.
9. Click “Add Rule”, put 8787 in the “Port Range” with the Source “Anywhere”. This is the RStudio Server port.
10. Hit “Review and Launch”. Click “Launch”.
11. Create a new key pair and name the Key pair anything you want. Download the Key Pair (.pem file). Keep this handy as this is how you’ll connect remotely to the server. Hit “Launch Instances”
That’s it for the AWS end. If your instance State is “running”, you have an EC2 instance running!
Connecting to the Instance via PuTTY and Setting up the Shiny Server
Open PuTTYgen and click load.
1. Change the extension type to all and navigate to where you store the .pem file from Step 11 in the previous section.
2. Open that file in PuTTYgen and click “Save private key” as a .ppk (you can choose to add a Key passphrase for added security. You will need to remember this passphrase each time you want to connect to the Instance).
3. Open PuTTY, go to the Category: Connection -> SSH -> Auth and load the .ppk file in the “Private key file for authentication:” input.
4. Head back to your EC2 Dashboard on AWS and click the “Connect” button. Copy the username@PublicDNS. In my case, it’s firstname.lastname@example.org
5. On PuTTY, go to the Category: Connection -> Data and in the login in details and input your username into the Auto-login username field
6. Go to Category: Session and enter your Public DNS into the Host Name. You will be required to change the Host Name each time you stop/start your Instance (this will be done when changing the Instance Type to install packages that require more than 1GB RAM).
7. You can now save the session by entering a name (I used Shiny-Putty). Click Open!
8. If done correctly, PuTTY will ask you if you trust the connection the first time you connect. Click Yes and enter in your passphrase (if you created one using PuTTYgen).
Congratulations! you should now have a remote connection into your Ubuntu instance where you can run Linux commands to install R, Shiny Server, and RStudio Server!
Setting up R and Shiny Server via Linux (PuTTY)
Installing CRAN and updating the ubuntu server. If any of the code fails, please omit the “-y” and respond “Y” (yes) when prompted.
sudo su -c “echo ‘deb http://archive.linux.duke.edu/cran/bin/linux/ubuntu trusty/’ >> /etc/apt/sources.list”
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys E084DAB9
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get install r-base -y
Installing Shiny, Rmarkdown and GGplot2 for the index page
sudo su – \
-c “R -e \”install.packages(‘shiny’, repos=’https://cran.rstudio.com/’)\””
sudo su – \
-c “R -e \”install.packages(‘rmarkdown’, repos=’https://cran.rstudio.com/’)\””
sudo su – \
-c “R -e \”install.packages(‘ggplot2′, repos=’https://cran.rstudio.com/’)\””
Getting gdebi-core and installing the latest version of Shiny Server (this could be out of date by the time you read it. Please go to Shiny Server’s official site to find out what the most recent version is).
sudo apt-get install gdebi-core
sudo gdebi shiny-server-184.108.40.2063-amd64.deb Once Shiny Server is installed, you should see a line in the terminal that says “shiny-server start/running process ####”. This means your server is up and running and you will be able to see the “Hello World” page by going to your IPv4 Public IP:3838 (you can find this in your EC2 dashboard on AWS).
Setting up the Rstudio Server
I’m sure by now, you’re sick of dealing with the terminal and would like to work in a more familiar environment. So let’s set up the RStudio Server!
1. Go to RStudio Server’s page and download Rstudio Server the same way you installed Shiny Server. be sure to use the Ubuntu 16 version. The current code below might be out of date when you read this blog.
sudo gdebi rstudio-server-1.2.5033-amd64.deb
2. You will need to create a user to log into RStudio Server. I have named my user rstudio. You can name it whatever you want.
sudo adduser rstudio
3. Enter a UNIX password. You don’t need to enter values for any of the other fields (Full Name, Room Number, Work Phone, Home Phone, Other).
4. Press “Y” to state the information is correct.
5. Now we have to give your user super user privileges
sudo vim /etc/sudoers 6. Scroll down to “#allow members of group sudo to execute any command”
7. Click “i” on your keyboard
8. Type in “%rstudio ALL=(ALL:ALL) ALL
Keep in mind that rstudio is my username which is why I typed it there. Replace that with the username you selected in Step 2.
9. Once done, press the “esc” key and type “:wq!” without the quotations and hit enter.
10. Go to your IPv4 Public IP:8787 (http://220.127.116.11:8787/ in my case) and log into RStudio Server using the user you created in step 2. You can now work in RStudio online on your Instance!
Uploading your App and Data
I already have a Shiny App that I locally coded and deployed to ShinyApps.io. I also am pulling data from a csv so I need to transfer my App.R file and my csv file where the data is stored.
1. Put in the Host and username info into fileZilla and click “Quickconnect”
2. You will then be prompted to add in a passphrase if you created one for your .ppk file. Enter it and hit “OK”.
3. Upload files into the /home/ubuntu/ folder (make sure your R file is called app.R)
4. Go to your PuTTY terminal and move your files from /home/ubuntu/ to /srv/shiny-server/ (this is where the Shiny Server is located.
sudo mv /home/ubuntu/app.R /srv/shiny-server/app.R
Note: you can add folders (ex. new_folder) within you /srv/shiny-server/ folder that will automatically be routed to IPv4 Public IP:3838/new_folder
5. Install all the packages for your application either using the RStudio Server or via the PuTTY terminal with the following code (where ‘package’ is the name of your package):
sudo su – \
-c “R -e \”install.packages(‘package’, repos=’https://cran.rstudio.com/’)\””
Note: If the installation of your package stalls/freezes, it means the current Instance does not have enough RAM to install it. You will need to go through the following steps to increase the RAM (this will cause you to be charged a small amount as AWS charges by second, but if you revert back to t2.micro once the installation is done, you’ll be fine).
Upgrading an Instance
1. In EC2, click on your Instance, then go to Actions -> Instance State -> Stop
2. Go to Actions -> Instance Settings -> Change Instance Type and temporarily change your Instance Type to one with more CPU and RAM.
3. Go to Actions -> Instance State -> Start
Note: Your IPv4 Public IP and DNS will change so use the new one in PuTTY and to go to the public URL
4. Install the packages you need. They should not stall/freeze now.
5. Repeat steps 1-3 but revert your Instance Type back to a t2.micro (or whatever free tier one you’re using).
Deploying your Shiny App
The last thing to do is to go to the url where your Shiny App is hosted (IPv4 Public IP:3838/) and check if your Shiny App is running. Unfortunately I got an error and was required to go into the logs and check what happened (this usually happens when a package isn’t install/loaded properly).
By using the VIM procedure we used above, I was able to dig into the logs and noticed that the DT package was not installed.
Now, if you go to my EC2 Instance, my Shiny App is running flawlessly without any auto shut down like ShinyApps.io. It is also scalable if I start to get more web traffic!