Setting up RStudio & Shiny server

9th of july 2017 vpsshiny server

My initial needs

Last year I made a simple app called uCountVisualizer which I published in the shinyapps.io platform. Unfortunately the free tier offered only 25 hours a month for all the apps hosted in it. When I started to work on my second app uGeoTagger it became clear that this tier was insufficient. So was the next one which do not gave enoughs hours/month to host an app 24/7.

Dean’s guide

For that reason I googled a little bit and found Dean’s article which provided guidance to install the free version of shiny server. Dean is a shiny expert and by following his guide you’ll be able to:

Setting up the custom domain

As described in Dean’s article you may set a custom domain for your shiny server. However in my case, my domain (alessiobenedetti.com) was already used to host this blog in the github platform, therefore I needed to set up subdomains to bind the shiny server (thelab.alessiobenedetti.com).

using a custom domain with GitHub Pages
setting up subdomains
  • first, in your registrar, you should add a new NS Record with the host portion of the subdomain, in my case I added the string “thelab” since my subdomain is “thelab.alessiobenedetti.com”. You should also add the DNS of your host, in my case I put “ns1.digitalocean.com
  • then you’d have to configure a new subdomain (I work with digital ocean and therefore I’ve made my setup in the “networking” section, as shown in the picture below)

  • after that you need to add the subdomain to your vps, by configuring the subdomain files in the following paths /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/

    1. inside the sites-available I created a file named thelab where I specified the path through the index file and the subdomain, as shown in the image below

    2. then in order to enable the configuration made in the previous step, inside the sites-enabled I created a symbolic link named thelab that references the thelab file in the sites-available directory. This can be done with the following statement ln -s /etc/nginx/sites-available/thelab /etc/nginx/sites-enabled/thelab. For more details you can refer to the following guide on digital ocean and this post that I found useful to create symbolic links
  • next, you need to configure your web server, in my case nginx, by adding a couple of new folders named thelab and thelab/html inside /var/www. In the resulting path /var/www/thelab/html you can add your index.html
  • lastly you have to restart nginx with the service nginx restart statement, in order to make those changes effective

Installing mongoDB

When I started developing the app I used a google sheet page as persistent storage. Despite R offers nice packages to manage google’s docs, I wasn’t satisfied of the loading time. That’s why I moved to a DB and chose Mongo. For simplicity I initially connected the app to an online istance of Mongo at the mlab.com hosting site. Later on, since the free tier of mlab.com doesn’t allow backups/restore, unless you pay for them, I decided to install my own istance of mongoDB. This guide was useful in doing that.

Installing RoboMongo

Browsing through mongoDB is made easier through a GUI browser. I found Robo 3T (formerly RoboMongo). Setting up the connection to the remote mongoDB was a little bit tricky because of the bad format of the private key. You need to create an Open SSH key (see this thread for support)

Setting up scheduled jobs to backup/restore the database

Last difficulty was automating the backup/restore jobs for the database. My wish was to create one daily backup, while keeping the last 5 backups day by day. I’ve had to dig on:

  1. changing the directory of the statements in an ubuntu distro
  2. setting up crontab job

Point 1) came from the fact that “shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is.” (stack overflow reference). Initially I was working with aliases but then I wasn’t able to set the crontab job. At the end I simply created a function in a sh script that was easier to schedule on crontab.

End of this journey! I’ve made a quick recap of the main points, hope that you can eventually find some useful hints!