IPython, IPython Notebook, Anaconda, and R (rpy2)


IPython and the IPython Notebook have vast potential beyond their traditional use in the Python scientific programming community. Specifically, the Notebook is a great learning tool, and that’s something I plan to highlight in an upcoming talk at the New England Regional Developer (NERD) Summit.

Because the NERD mission is to reduce barriers for people entering IT (as opposed to having them waste years of their lives untangling Python package dependencies), the plan is to demo everything using the Anaconda Python installation. Overkill maybe, since even on Windows installing Python and IPython isn’t too terrible. That said:

  • It’s important for those who aren’t proficient on the command line to jump right in.
  • If people get hooked on Python and want to do more, they’ll have the important packages at the ready (Anaconda includes some important machine learning packages that are missing from the free version of its competitor, Enthought Canopy).

To understand the worst case pain scenario before recommending Anaconda to beginners, I installed it on Windows. So far, so good.

The only snafu I’ve hit so far is trying to get the IPython–>R integration working. This isn’t really a feature for beginners, but I want to show it because R is heavily used at local universities.

To be clear, this mess isn’t an Anaconda problem. However, if you’re using Anaconda on Windows and want to use IPython’s rmagic extension, here’s how.

  1. Install R.
  2. Add the directory with the R executables to your PATH. It has to be the directory with executables, not the main R folder. On a 64-bit machine, the directory is something like C:\Program Files\R\R-3.1.0\bin\x64.
  3. Add these two environment variables (h/t):
    • R_HOME (path of the main R folder, e.g. C:\Program Files\R\R-3.1.0)
    • R_USER (your Windows username).
  4. Restart Windows.
  5. Modify your Windows Python install registry key to point to your Anaconda Python location instead of the default Python installation (h/t). If you followed the default Anaconda install prompts (which installs for the current user, rather than all users on the machine), you’d change HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\2.7\InstallPath.
  6. Download and install Dr. Gohlke’s rpy2 Windows binary (grab version 2.4.0 or higher): http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2.
  7. Change the registry key from step 5 back to it’s original value.
  8. Open an IPython notebook or terminal and load the rmagic extension:
    %load_ext rpy2.ipython
  9. You should be able to test everything out using this sample code.

For comparison, these are the Ubuntu instructions.

  1. Install R:
    sudo apt-get install r-base r-base-core r-base-html
  2. Install rpy2:
    pip install rpy2

3 Responses to IPython, IPython Notebook, Anaconda, and R (rpy2)

  1. jrovegno December 4, 2014 at 4:49 pm #

    I agree I also started with “Canopy Python distribution” but when I tried to install something extra or update something to the latest version available, things became very difficult.

    Then I tried “Anaconda Python distribution” and using “conda” was as natural as using “apt” even creates an extra layer of facility, to create environments and packaged for Binstar.

    Some questions about point 2 and 3:
    If I have installed a win-32 conda plataform
    ¿Is this correct?

    # system-wide environmen
    setx PATH “%PATH%;C:\Program Files\R\R-3.1.2\bin\i386” /M

    # user environmen
    setx R_HOME “C:\Program Files\R\R-3.1.2”
    setx R_USER “%USERNAME%”

    Then I’ll try to install “rpy” with “conda”, but I’m not sure about to set variables correctly.

    pip install -i https://pypi.binstar.org/pypi/simple rpy2


  2. jrovegno December 6, 2014 at 6:37 am #

    I couldn’t install rpy2 from source, to many tweaks ( VCForPython27, Cannot open include file: ‘R.h’, etc.)
    I have installed Dr. Gohlke’s rpy2 Windows binary
    Then installed singledispatch with pip
    pip install -i https://pypi.binstar.org/pypi/simple singledispatch
    Now %load_ext rmagic works fine.

    The only problem is R’s output generate an annoying space.

  3. Elodie Royant September 29, 2015 at 4:51 pm #

    I have installed conda on an ubuntu distribution.
    pip install rpy2 didn’t work out for me (error: command ‘gcc’ failed with exit status 1)
    The solution is : conda install -c asmeurer rpy2

Leave a Reply