Getting IPython rmagic to Work on Windows

Posted on Tue 29 April 2014 in tech

After updating to IPython 2.0, I decided to get rmagic working on a Windows 8.1 install. These instructions should probably work with IPyhon 1.X, too, although I have not tried.

R on Windows

Behind the scenes, rmagic assumes that a working version of R is available on the Windows PATH. The most recent Windows installers for R 3.0 do not modify the PATH variable. This is actually a good thing because it allows you to install multiple versions of R in whatever 32-bit or 64-bit flavors you'd like, and they can all co-exist peacefully. That said, to get rmagic to work you'll have to put one of them on the PATH by hand.

Which Flavor?

The flavor of R you put on the PATH must match the flavor of Python you are targeting. More specifically, if you're running 32-bit Python, place a 32-bit flavor of R on the path. The locations of these binaries will probably look something like this:

  • 32-bit R: C:\Program Files\R\R-3.0.3\bin\i386
  • 64-bit R: C:\Program Files\R\R-3.0.3\bin\x64

It's critical to go all the way down to these flavor-specific directories since it's really R.dll that needs be available to rmagic. Why is that? Read on.

rpy2

In addition to a working R install, rmagic in IPython 2.0 uses rpy2 as the connection between Python and R. Installing rpy2 on Windows is not quite as simple as it perhaps should be, and Stack Overflow is littered with questions about failed attempts at using pip or easy_install. After culling through several of those posts, I happened upon one that actually had the right magic beans. The post is a little dated, so here's a refreshed summary of the simplest path to success today:

  1. As described above, pick a suitable flavor of R, and put it on the PATH.
  2. Pull down the (unofficial) rpy2 binaries from Christhoph Gohlke's treasure trove. Make sure you pick the right Python version of the binaries to match your Python flavor, including Python version number.
  3. Install those binaries into whatever Python environment you intend to use. (Did you know you can install binary packages into any Python environment, even those not in the Windows registry? That includes an Anaconda install.)
  4. Create an environment variable called R_HOME, and point it to the base R install directory. If your paths look like those above, that would be "C:\Program Files\R\R-3.0.3" or whatever matches your R version.
  5. Create an environment variable called R_USER, and set it to your Windows username. This looks to be in line with recent discussion on the R development issues list.

Once you've done all that, open up a fresh command prompt and verify that all your environment changes were correctly set. Then fire up IPython notebook, issue

%load_ext rmagic

in a cell, and the rest of the guidance in the IPython documentation should then apply.

Some Issues

Unfortunately, it's not necessarily all roses after that. There are still issues with getting some information back and forth across the R-Python interface in pristine format. Sometimes the output from R commands doesn't completely make it into the notebook and ends up being printed in the command shell window from which the notebook is running. But aside from a few minor inconveniences, it doesn't look like there are any real errors happening.