RStudio Shiny Server in AWS

AWS/EC2 Setup

  • Step 1- Amazon Machine Instance: Ubuntu
  • Step 2 – Instance Type: m4.large
  • Step 3 – No changes
  • Step 4 – Storage: 30 Size GiB
  • Step 5 – No changes
  • Step 6 – Security Group Name: ShinyClick Add Rule, select Type: All Trafic | Source: My IPOptional: If you want to open your Shiny server to the world – Click Add Rule, select Type: Custom TCP Rule | Port Range: 3838 |Source: Anywhere

    Optional: If you plan to have to publish apps from a RStudio server within the local network (VPC) – Click Add Rule, select Type: All Traffic | Source: Custom and type the the first two numbers in the IP address followed by ‘.0.0/16’ (So,if your Shiny server’s internal IP address is 172.31.2.200 then you’d enter 172.31.0.0/16 This gives every server in your VPC access to your Shiny server and to each other)

  • After clicking Launch the “Select existing pair or create a new pair” screen will appear, select:Create a new pairKey pair name: shiny

    Click Download Key Pair

    Save the file

    Click Launch Instances

  • Go to the Instances section in the EC2 Dashboard section and click on the new instance
  • Copy the Public DNS address, from here on we’ll refer to it as MY_PUBLIC_DNS

Key and Connection setup from laptop

AWS provides the step-by-step instructions for this part:

Shiny server configuration

Install R

We need to update the package list so that Ubuntu installs the latest R version. Here is the reference: https://cran.r-project.org/bin/linux/ubuntu/

Tip The terminal commands inside the boxes can be copied and pasted into your terminal session. In putty you can use right-click as the “paste” command.

sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list'
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

Install Shiny

The steps below install the current version. To find updated instructions navigate to: https://www.rstudio.com/products/shiny/download-server/ and select Ubuntu 12.04 or later

sudo su - -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""
sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.4.4.801-amd64.deb
sudo gdebi shiny-server-1.4.4.801-amd64.deb

Install RStudio

Why install RStudio? I’m using RStudio to upload and test my apps along with any other supporting files. This removes the need to setup complicated security and FTP.

The steps below install the current version. To find updated instructions navigate to: https://www.rstudio.com/products/rstudio/download-server/ and select Debian/Ubuntu

wget https://download2.rstudio.org/rstudio-server-0.99.903-amd64.deb
sudo gdebi rstudio-server-0.99.903-amd64.deb

We need to add a default user that can log on RStudio. I’m going to use ‘rstudio’ as the name but it can be whatever you’d like. The second command give the user ‘sudo’ (read Admin) access to the server. The third line is key, it makes it where you don’t have to type ‘sudo’ before any file/folder command. This command makes it possible for RStudio to create new folders and files in the Shiny server.

sudo adduser rstudio
sudo usermod -aG sudo rstudio
sudo chown rstudio /srv/shiny-server/

type rstudio as the password

Pre-install needed packages

Any package that an ‘Shiny App’ uses will need to be pre-installed, or the will crash. Here are a few we will load before we start using the server. If you need any other package, just follow the instruction pattern, replacing the name of the desired package.

You can always install more packages later via a terminal session

sudo su - -c "R -e \"install.packages('rmarkdown', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('ggvis', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('ggplot2', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('compat-gmp4', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('compat-libffi5', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('data.table', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('ggplot2', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('stats', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('plyr', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('maps', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('stringr', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('mapproj', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('ggExtra', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('ggthemes', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('stats', repos='http://cran.us.r-project.org')\""
sudo su - -c "R -e \"install.packages('flexdashboard', repos='http://cran.us.r-project.org')\""

Test server connnectivity

Publishing apps

Publishing from a laptop

  • Navigate to the RStudio server and log on as ‘rstudio’
  • Confirm that the latest version of R is loaded (As of today is 3.3.1)
  • Click FileNew FileShiny Web App
  • Application Name: my_test
  • Click Browse
  • Click on the Go to directory button, identified as […]
  • Type the following (exactly as is in here): /srv/shiny-server/
  • Click Choose and then click Create
  • Navigate to your new app by going to: http://MY_PUBLIC_DNS:3838/my_test

Publishing from a RStudio server in the local network

The following method sets up a single user in the RStudio server to have ‘write’ access in the Shiny server. Using Samba, we will create a shared folder inside the RStudio server user that maps directly to the Shiny server shiny-server folder. In order for RStudio to create folders and files, a specific user needs to logged on in the RStudio server. This is why we are not putting the ‘shiny-server’ mount point in a more generic location. For purposes of this document, I’ll use shiny-user as the user.

The assumption is made that the ‘client’ RStudio server is running an Ubuntu (Debian) distribution.

Make sure to perform the second optional security setting in the ‘AWS/EC2 Setup’ section

The plan is to:

  1. Create a user in the Shiny and RStudio server with the same name and password
  2. Install and configure the Samba server in the Shiny server
  3. Install and configure the Samba client in the RStudio server
  4. Point the ‘shiny-server’ folder to a local folder in the RStudio server’s user

In the Shiny server

  • Open a new terminal session in the Shiny Server
  • We’ll create the new user, give it ‘admin’ access and then we remove the need for it to use the ‘sudo’ command
    sudo adduser shiny-user
    sudo usermod -aG sudo shiny-user
    sudo chown shiny-user /srv/shiny-server/
    sudo chgrp sudo /srv/shiny-server/
  • Installing and configuring Samba
    sudo apt-get install samba
    sudo smbpasswd -a shiny-user [typed password twice]
    sudo nano /etc/samba/smb.conf
  • Add the following lines at the end of the file:
    [shiny-server]
    comment = Shiny Server Share
    path=/srv/shiny-server/
    browsable = yes
    valid users = shiny-user
    read only = no
  • Close the file by typing CRTL+X, then Y and enter
  • Restarting the Samba server
    sudo service smbd restart

In the RStudio server

  • Start a terminal session in the ‘client’ RStudio Server
  • Create the new user
    sudo adduser shiny-user
  • In a web browser, navigate to the RStudio server and log on as ‘shiny-user’
  • Create a folder called shiny-server
  • Switch back to the terminal session in the RStudio server
  • We’ll install and configure the Samba client
    sudo apt-get install cifs-utils
    sudo nano /etc/fstab
  • Add the following lines at the end of the file:
    //[ Shiny server's Internal IP Address]/shiny-server  /home/shiny-user/shiny-server  cifs  username=shiny-user,password=shiny-user
  • Close the file by typing CRTL+X, then Y and enter
  • ‘Mount’ the share, which means that the internal folder now points to the folder in the external Shiny server
    sudo mount -a
  • Go back to the web browser that has the RStudio session
  • Click on the ‘shiny-server’ folder
  • To confirm that the permissions are correct, create a new Shiny app, called server_test in the shiny-server folder
  • Navigate to your new app by going to: http://MY_PUBLIC_DNS:3838/server_test