XD blog

blog page

ipython, notebook, python, r, rmagic

2014-09-07 Four issues I had when using IPython, R and rmagic

I wanted to use R through IPython notebook to mix Python and R. I did it on Windows and I guess it requires to be more careful. I went through four issues.

%load_ext rpy2.ipython, not %rmagic

I'm using the latest version of IPython 2.2. Many examples on the web use %rmagic. But it only works with %load_ext rpy2.ipython.

ImportError: DLL load failed

pywin32 sometimes fails to find a DLL. A path is missing in the environment variable PATH. So I made a function which fixes that: import_pywin32. Just look at the code to see what is missing. The function must be called before calling R.


Versions of rpy2, Python, R must match. On Windows, we install setup most of the time to avoid compilation issues. The setup of rpy2 is built for a specific version of Python and R. They must match. On Windows, check #rpy2.

Missing packages

I needed a package not installed by default with R (ie actuar). The instruction library ("actuar") does not raise an exception when it fails. The output is just missing if you have any expected prints or graphs. That's why the following instruction must be ran once just to install it: install.packages("actuar"). So if you use a package and if you see unexpected things, just check all packages you need are installed.

Missing output

The R function print displays information in server command windows and not in the notebook. I did not find a way to get it back by redirecting it unless it is in a file with the package sink.

<-- -->

Xavier Dupré