I’ve been mucking around in R quite a bit lately and have grown tired of all the annoying configuration changes that are required to take your data/analysis from one machine to another. Even with my code in a Git repository, I was still dealing with package inconsistencies across R environments, differing file paths, and more - needless issues that took me away from the analysis at hand.
Combined with a desire to have an always-on machine available to host Shiny apps, I decided to provision my own cheap, always-up cloud server to run RStudio and Shiny Server. This server would allow me to ditch my patchwork of computers and environments and also provide an ability to host Shiny apps, making it perfect to support this blog.
If you’re looking to host your own RStudio Server or Shiny Server, and especially if you’re looking to run them in a low-cost AWS Lightsail environment, take a peek at my takeaways from below! I think you’ll find the “swap” and “R package” sections to be particularly helpful if your configuration is similar to mine.
Since my time with AWS’s Free Tier is over (RIP), the cheapest options available for server hosting were running a t2.nano EC2 instance ($4.75/month), Digital Ocean Droplet ($5/month), or Amazon Lightsail instance ($5/month). I ended up selecting Lightsail for the convenience it offers newbies or people starting off with cloud computing (simplified setup, in-browser SSH connectivity), and ability to connect with my existing AWS services.
The lowest specifications work fine for what we are doing, and at only $5 per month it’s extremely affordable. Make sure to choose Amazon Linux and to open ports 8787 (RStudio Server) and 3838 (Shiny Server) in your Security Group - attaching a Static IP will also make connecting to your server easier in the future!
Configure the Instance
Either using SSH, or Lightsail’s built-in SSH connection through the browser (pretty cool!), log into your server remotely. Start off by running a system update:
sudo yum update
Then add a new user for RStudio, which authenticates its web login with the local user groups on your server:
sudo useradd USERNAME sudo passwd USERNAME
Install RStudio Server
Following the instructions on the RStudio website for RHEL/CentOS, execute the following commands to download and install RStudio Server.
wget https://download2.rstudio.org/rstudio-server-rhel-1.1.442-x86_64.rpm sudo yum install rstudio-server-rhel-1.1.442-x86_64.rpm
Now, navigate to your server via http://YOUR_IP:8787 to access RStudio! Login with the credentials you created earlier and you should see the classic RStudio interface.
Add Swap Partition
If you’re anything like me, once you get into your R environment you’ll start installing a bunch of packages like “tidyverse” and “shiny” that you use for data analysis. However, you will likely run into some unexplained failures during installation, especially related to “Rcpp” (a library used to connect R with C++). After quite a bit of digging, I found out that these failures are usually caused by a lack of RAM on your server, which makes sense given that the base AWS Lightsail server has only 512 MB of RAM.
Short of upgrading your instance, the easiest way of getting around this issue is to simply add a swap partition to augment your physical memory and save pages in memory (albeit at a much slower speed then RAM). To create a 1 GB swap partition on your main disk, which loads on boot, follow the steps below:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 sudo mkswap /swapfile sudo swapon /swapfile
Then add the following line to your /etc/fstab to create a mountpoint moving forward:
/swapfile swap swap defaults 0 0
Install R Packages as Root User
Another issue I encountered when installing Shiny Server was that the R packages I installed through the RStudio interface weren’t being recognized by Shiny. That is - even with Shiny installed via the RStudio interface, Shiny Server would have multiple errors when I pulled up the welcome page upon install.
After some more digging, I found out that R has a number of quirks regarding package installation and user accounts. Namely, all packages are only available to the user accounts within which they are installed, except the root account, whose packages are available to all accounts. As an example, if I install the “tidyverse” package as the user “conor”, that same package will not be available to the user “michael”. However, if I install “tidyverse” as the root user, both “conor” and “michael” will have access to the “tidyverse” library.
To install R packages as the root, simply connect via SSH and execute the following commands of the following format at the command line. These lines below help install “shiny” and “rmarkdown” to make the Shiny Server run properly:
R -e "install.packages('shiny', repos='http://cran.rstudio.com/')" R -e "install.packages('rmarkdown', repos='http://cran.rstudio.com/')"
Install Shiny Server
With swap space added, RStudio online, and Shiny packages installed and available, you’re ready to install Shiny Server. Following the instructions on the RStudio website for RHEL/CentOS, execute the following commands to download and install Shiny Server.
wget https://download3.rstudio.org/centos5.9/x86_64/shiny-server-126.96.36.1995-rh5-x86_64.rpm sudo yum install --nogpgcheck shiny-server-188.8.131.525-rh5-x86_64.rpm
After installation completes, navigate to your server via http://YOUR_IP:8787 to access RStudio!