Written by Peter Rosenmai on 16 Nov 2014.
Here's how I installed the rpy2 module for communicating between R and Python. I did this for R version 3.0.2, Python version 3.4.1, and rpy2 version 2.4.4 on a 64-bit machine running Windows 7.
First, I got the full pathname of my R executible by right-clicking the R icon in my Start menu and selecting Properties. It was: C:\Program Files\R\R-3.0.2\bin\x64\Rgui.exe. And it only took a moment of poking around to find the full pathname of my Python executible: C:\Anaconda3\python.exe.
Time to look at my system variables. I right-clicked on Computer in my Start menu and selected Properties; I then clicked on Advanced System Settings in the window that appeared. In the Advanced tab of the System Properties window, I clicked the Environment Variables button. The lower half of the resulting Environment Variables window showed my system variables.
The first system variable I had to deal with was Path. It didn't include the directory in which my R executable sits, so I added it: C:\Program Files\R\R-3.0.2\bin\x64\.
I then added an R_HOME system variable and set it to the top level directory of my R installation: C:\Program Files\R\R-3.0.2\.
And I added an R_USER system variable and set it to the directory that the rpy2 module would install into: C:\Anaconda3\Lib\site-packages\rpy2\.
Having run into problems installing rpy2 using pip, I instead installed rpy2 and its dependent module pywin32 using the installation executables available at http://www.lfd.uci.edu/~gohlke/pythonlibs/. (For my own interest, I then had a look at pywin32-wininst.log in the base directory of my Python installation—C:\Python\Anaconda3—for detailed information about the installation.)
Opening a command line window as administrator, I went to the base directory of my Python installation (C:\Python\Anaconda3). I then ran this:
I tested rpy2 using the following code:
I got this error:
So I had a look in that tempfile. It contained a single line:
Good. R was at least doing the required calculation.
I found the file robject.py in C:\Python\Anaconda3\Lib\site-packages\rpy2\robjects and looked for this code:
I changed that code to:
I reran my hello-world code and found that the "s = str.join(os.linesep, s)" assignment in the above code was giving me this error:
I made another change to the above code:
And then my hello-world code worked: