Installing an R kernel for IPython/jupyter notebook 3 on OSX

Update (April 3, 2016): Since posting this a little over year ago a great deal has progressed on this front. Notably, Jupyter has moved on to 4.x, and as pointed out in the comments, you can follow specific instructions on installing the R kernel for the Jupyter notebook 4.x+ here.

Nonetheless, the information below may be useful for walking through the more basic steps needed if you are relatively new to programming or software development. It also may explain steps that are just listed as instructions in the official page. The hope is that there is value in this post, even though there are new resources for ealing with newer versions of the notebook.

Initial instructions & preliminaries

Examine the IRkernel instructions (i.e., read the README.md file). Below are a few preliminary questions that you might run up against if you are new to programming[1]. If you are an experienced programmer and know the meaning of "terminal", brew, pip, git, IPython, or Jupyter, you may wish to skip to the section called Installing R and devtools.

  • Do you know how to open a terminal window? Try pressing ⌘+space to open Spotlight[2] (a OSX tool) and type “Terminal” into the bar that opens.
    • What do you use as your terminal emulator? If the regular terminal, I would suggest downloading and installing iTerm2, it will make your life easier.
  • Do you know how to use brew? If not, read this explanation.
  • This entire walkthrough will not be very useful unless you already have python(2.7 or 3) and IPython installed. Go to the IPython installation site to set up IPython.
    • Note, python 3 and IPython 3 are very different things.
      • python 3 is the officially sanctioned version of python which is contrasted often with python 2.7 which has extensive library support, but which will not be developed any further by the core python development team.
      • IPython 3 is version of IPython that was recently released. IPython stands for "interactive python" and is equipped with a powerful in-browser interactive editor known as the IPython notebook. As you might guess from the title of this walkthrough, this editor can run R. The project has recently split into two parts IPython and Jupyter. The details of this split have largely to do with the former focusing on python specifically with the latter focusing on language agnostic capabilities (such as the R kernel that we will be installing). IPython 3.x will be the last release of IPython that includes the notebook. More information about the two projects can be found at the IPython site and the Jupyter site.
    • If you already have python, but do not have IPython or the IPython notebook installed, try running pip install "ipython[notebook]".
      • If you don’t have pip or know what pip is, read this website. If you want to rush blindly into the future without knowing what exactly you’re doing, then you can run easy_install pip(or if you have root access[3] sudo easy_install pip), which will allow the previous command to work.

Installing R and devtools

Run R from the command line to open an R terminal, then, run the command install.packages("devtools").

  • If you do not have R installed, run brew install r. Be aware — because of the number of required packages, this can take a while to finish.
  • If you have R installed, but you run into issues regardless, try instead running brew upgrade r.
  • Hint: If you don’t know how to run R from the command line, but you have installed try typing R or r in your bash terminal (i.e., capitalization doesn’t matter).
  • If after doing this, you still cannot get install.packages("devtools") to work after installing via brew. Go to R’s homepage and download and install the latest version from their pkg installer (at time of writing this is R 3.1.2).

Dealing with a common issue: rzmq

At this point if you continue with the instructions at the IRkernel github page, you may run into the issue that is discussed here regarding the lack of availability of rzmq given the current way that install_github works (especially since it is also absent from CRAN).

  • To get this you will need to have a working version of git, which comes with the XCode command line tools and if you attempt to use git from the command line and you do not have it installed, it will prompt you to install it.
    • Even better would be to follow the instructions here that describe how to update git to a more recent version than that installed by Apple while moving your Apple version of git to its own separate directory.

  • First, check that you have basic bindings for ZeroMQ (which is what zmq refers to). Do not worry if it says you already have them, if you want to update them instead, just change install to update.

  • brew install czmq zmq
  • With that in place you want to recursively clone the git repository that has the relevant rzmq build, which you can do by putting the following line into your terminal (which will clone it to your current working directory, which you can see by typing pwd).

    git clone https://github.com/armstrtw/rzmq.git --recursive
    
  • And then without navigating to any other directory (this is key for the work around to work), `r`, and once the R terminal is running, paste the following commands:

    install.packages('RCurl')  
    library(devtools)  
    install_local('./rzmq')  
    install_github('IRkernel/repr')
    install_github('IRkernel/IRdisplay')  
    install_github('IRkernel/IRkernel')
    
  • If you are using IPython 3 then insert the following command as well.

    IRkernel::installspec()
    

Payoff: Checking to make sure it works.

Go to your command line prompt once again (i.e., open a new Terminal or iTerm2 window), and run the following command:

ipython notebook

This should open a window in your web browser of choice. If you have IPython 3 installed, in the upper right hand corner you should see menu-button that says “New ▾” (see the item pointed to in the following picture):


Newnotebook


If everything has worked out well, click this menu-button you should see (at least) the following options:


ipython3withR


Which means all you need to do now to run R in a jupyter/IPython notebook is to click the letter “R”. A new window should open with an R kernel that is ready to go.

If you’ve gotten this far, well done! Enjoy your new R environment!

Updated: 2015_20_7_1648. Thanks to Alistair Walsh, Andrew Lonsdale and johnlaudun for their comments which helped me improve this since originally posting it.


  1. My aim with this tutorial is to make it accessible to literally anyone who can read English text and has access to OSX and the internet. This means that some of my explanations may seem rudimentary, but I find that too few instances of someone actually going through  ↩

  2. If you have another application launcher installed then this will also work. I merely assumed that people who do not know what the terminal is probably do not know that launchers exist, though I surely could be wrong.  ↩

  3. sudo is an abbreviation for “substitute user do” though it is also commonly said to be an abbreviation for “super user do”. The wikipedia entry on sudo does a better job of explaining this than a footnote to a 3rd level indented list item ever could(see also).  ↩