Skip to content

Shiny Server tutorial

driu edited this page Apr 7, 2013 · 3 revisions

This document provides step by step instructions how to set up Shiny Server on your SRCF account and has been kindly provided my Daniel Richman.

The tutorial can also help in situations where you have access to a web server but not as administrator (i.e. cannot run sudo). Note that then you may need to install/update other components, see Shiny Server's webpage for details.

Introduction

Shiny Server requires new versions of R and nodejs so these will need updating. Also, it not only wants to be installed system wide but also to run as root, which we can't do. However, luckily, we can install R and nodejs into a prefix in your home directory, and work around shiny-server's design, without too much effort.

In the following replace userID with your username, and PORT with the port on which you will run your server. You will need to choose a port number that no one else is using; the default is 3838, so the next person to set one up may choose 3839, and so on. Please contact the admin to obtain a port that is not yet used by another user.

Preparation

First, open a shell on pip and lower its priority, since we'll be doing a couple of long compiles.

$ renice -n 10 -p $$

Create a directory to store this in. You'll need about 400MB of free space to set this up, then we can clean up a little bringing that down to 100MB.

$ cd /home/userID
$ mkdir shiny-server
$ cd /home/userID/shiny-server

And a subdirectory for 'installing' things into.

$ mkdir prefix
$ mkdir prefix/bin

Edit the file prefix/bin/activate in your favourite text editor

$ nano prefix/bin/activate

and make its contents to be:

export PATH=/home/userID/shiny-server/prefix/bin:$PATH

If you want to paste this text into nano, the usual keyboard shortcuts (Ctrl+C, Ctrl+V) may not work, depending on your terminal. Try using the mouse context menu.

To save the file contents press Ctrl-O and Enter, then Ctrl+X to close the nano text editor and return to the console.

Now type

$ source prefix/bin/activate

Installing the software

First, we need a newer version of R (apparently).

$ cd /home/userID/shiny-server
$ wget http://cran.rstudio.com/src/base/R-2/R-2.15.3.tar.gz
$ tar xvf R-2.15.3.tar.gz
$ cd R-2.15.3/
$ ./configure --prefix=/home/userID/shiny-server/prefix/
$ make
$ make install
$ cd /home/userID/shiny-server

Note that make install does not require root, since we set the prefix (which is usually /usr) to be inside your home directory.

This will take a little while. Once it's done, type

$ R

and you should get this line:

R version 2.15.3 (2013-03-01) -- "Security Blanket"

If you don't, abort! Something's gone wrong.

Now, at the R prompt:

> install.packages('shiny', repos='http://cran.rstudio.com/')

Again, note lack of root. Since R was installed to the prefix inside your home directory, it should attempt to install shiny in there too.

It should complete without asking you where to install shiny; specifically, it shouldn't ask you if you want to install it in ~/R/x86_64-pc-linux-gnu-library/2.14

Exit R

q()

Next, nodejs:

$ cd /home/userID/shiny-server
$ wget http://nodejs.org/dist/v0.10.2/node-v0.10.2.tar.gz
$ tar xvf node-v0.10.2.tar.gz
$ cd node-v0.10.2
$ ./configure --prefix=/home/userID/shiny-server/prefix/
$ make
$ make install
$ cd /home/userID/shiny-server

And now shiny-server.

$ npm install -g shiny-server

Make shiny-server's log directory

$ cd /home/userID/shiny-server
$ mkdir log

Configuring

Now we need some extra files to configure shiny-server and work around its strange lust for root.

Edit shiny-server.config and make the contents be:

run_as userID;
server {
  listen PORT;
  location / {
    site_dir /home/userID/public_html/shiny;
    log_dir /home/userID/shiny-server/log;
    directory_index on;
  }
}

Somewhat irritatingly, shiny-server expects to be run as root, and then tries to execute R using 'su', which doesn't work at all... so let's give it a fake 'su' that does nothing instead:

Edit the contents of prefix/bin/su

#!/usr/bin/python
import sys, os
if len(sys.argv) != 7 or \
   sys.argv[1:6] != ['-s', '/bin/sh', '--', 'userID', '-c']:
    raise ValueError("Unexpected command")
os.execve('/bin/sh', ['sh', '-c', sys.argv[6]], os.environ)

and make prefix/bin/su executable

$ chmod +x prefix/bin/su

Finally, a quick script to start the server. Edit start-server

#!/bin/sh
cd /home/ad610/shiny-server
. prefix/bin/activate
exec shiny-server shiny-server.config

And now make it executable:

$ chmod +x start-server

Let's set up a directory in public_html/ to be served by shiny_server

$ cd /home/userID/public_html
$ mkdir shiny

Edit contents of shiny/.htaccess

RewriteEngine On
RewriteRule (.*) http://localhost:PORT/$1 [P,L]

Starting the server

When I was testing this, I installed one of the examples on the shiny website:

$ mkdir shiny/snakes
$ cd /home/userID/public_html/shiny/snakes
$ curl https://gist.github.com/wch/4027145/download | tar xvz --strip-components=1

Now, let's start the server

$ cd /home/userID
$ screen -d -m shiny-server/start-server

If you're unfamiliar with screen, it keeps programs running in a virtual terminal that you may 'attach' to, like so:

$ screen -aAx

And then detach by pressing Ctrl-A, releasing, then d. You can of course shutdown the server with kill or by attaching and issuing Ctrl-C.

Visit http://userID.user.srcf.net/shiny (and cross your fingers).

Housekeeping

Provided it works, we can remove some unneeded files.

$ cd /home/userID/shiny-server
$ rm -rf {R-2.15.3,node-v0.10.2}{,.tar.gz}

Scheduling

You probably also want to start the server if we have to reboot pip, so, edit your crontab:

$ crontab -e

and add the line

@reboot screen -d -m shiny-server/start-server

Running R via console

If you later want to use the R console, after connecting to pip you will need to issue

$ source /home/userID/shiny-server/prefix/bin/activate

first, before typing

$ R