Authoring R in Jupyter Notebooks on Docker

Although you can install Jupyter + R + kernels on a PC (at least on OSX) I will focus on using Docker. This provides the same environment regardless of OS. See my previous post on creating a Docker definition for a local Jupyter server.

Using R in Jupyter Notebooks

The major choice you face is whether to :

  1. Use a Python kernel with option for R cell magics using rpy2
  2. Use the newer R kernel

The principal advantage of using a Python kernel is having the ability to mix R with Python and shell commands. For writing pure R documents, the R kernel provides a cleaner "R only" look and better inline command completion and help.

Example Notebooks are provided in the Notebooks directory. Github enabled in-place rendering of these recently so you can see the notebook JSON rendered as HTML. There are still some teething problems with graphics payload in these notebooks but the IRKernel team are onto it!

Git-Oriented Workflow

My workflow for using a Dockerised notebook server to author R notebooks is:

  1. Create a new running container with port forwarding of the notebook server port 8888 and the location of my working directory on host mounted at e.g. /Documents
docker run -it -v /Users/johnmccallum/Documents:/Documents \
 -p 8888:8888 cfljam/pyRat
  1. Navigate using the Notebook browser interface to host location where I have a git repository.
  2. Create notebooks using R or python kernels.
  3. Commit notebooks to repo, where they are now rendered as HTML
  4. Optionally store notebooks as GitHub secret Gists for sharing rendered links via Nbviewer, and tag these using GistBox.
  5. Share and discuss output with collaborators via pull requests and/or Gist links

Using R in a Python kernel with RPy2

  1. if you don't have rpy2 installed, open up a terminal and eter:
pip install rpy2
  1. In a Python 2 Notebook and type in a cell %load_ext rpy2.ipython %pylab inline .. this will load in R interface and also toggle inline graphs

  2. Read the Rpy2 cell magics documentation

  3. Run R with single line magics (%R) or cell magics (%%R)

Using Rkernel Notebooks

Just write R! Calm

Sharing Via Gists

See my earlier post on gisting notebooks and the self-documenting gist.

You can also try rendering self-contained notebooks Gists at http://lambdaops.com/gistexec . Try my demo notebook

Thanks!

.. to the Rocker and Jupyter teams who are doing an amazing job bringing us these expressive tools for data science. Also to Kyle @rgbkrk for the amazing gistexec

Links

Written on June 23, 2015