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 :
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:
- 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
- Navigate using the Notebook browser interface to host location where I have a git repository.
- Create notebooks using R or python kernels.
- Commit notebooks to repo, where they are now rendered as HTML
- Optionally store notebooks as GitHub secret Gists for sharing rendered links via Nbviewer, and tag these using GistBox.
- Share and discuss output with collaborators via pull requests and/or Gist links
Using R in a Python kernel with RPy2
- if you don't have rpy2 installed, open up a terminal and eter:
pip install rpy2
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 graphsRun R with single line magics (%R) or cell magics (%%R)
Using Rkernel Notebooks
Just write R!
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