13

I know there are quite a few posts on getting up and running with rpy2 on windows 7 32 bit. I have referenced a good number of them and attempted their solutions, including the use of PypeR.

I dont explicitly have a R_HOME variable set in my path, but per this question, I confirmed that R is in my PATH (I can type R at the command line and get R to run) and even copied all of the files from the i386 folder to the parent bin folder.

My issue is pasted below. Any thoughts?

In [5]: from rpy2 import robjects
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-5-1f019d00d232> in <module>()
----> 1 from rpy2 import robjects

C:\Anaconda\lib\site-packages\rpy2\robjects\__init__.py in <module>()
     16 import rpy2.rlike.container as rlc
     17
---> 18 from rpy2.robjects.robject import RObjectMixin, RObject
     19 from rpy2.robjects.vectors import *
     20 from rpy2.robjects.functions import Function, SignatureTranslatedFunction

C:\Anaconda\lib\site-packages\rpy2\robjects\robject.py in <module>()
      3 import rpy2.rinterface
      4
----> 5 rpy2.rinterface.initr()
      6
      7 import conversion

RuntimeError: R_HOME not defined.
Community
  • 1
  • 1
Btibert3
  • 38,798
  • 44
  • 129
  • 168

2 Answers2

14

I confirm this issue is related to R_HOME variable not defined.

In my opinion, all of these issues are caused because the developers of rpy2 package haven't documented properly the requirements of their software:

  • Install R
  • Create R_HOME system variable
  • Add R_HOME\bin to the PATH, in order to execute R from python
  • Add R_HOME\bin\x64 to the PATH, in order to load R.dll
  • Install package tzlocal
Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
Pablo
  • 141
  • 1
  • 2
  • 4
    -2 what command do you use to install r? Does it matter if I install it on python 2.7 or 3? I am trying to have the installation done on jupiter notebook. specifically, Google Cloud Datalab Thanks, Eila – eilalan Feb 18 '20 at 13:23
  • One additional step I had to do is get a POSIX `sh` in the PATH (like MSYS2 or git4windows) – Refael Ackermann Mar 10 '20 at 21:49
  • 1
    If you already installed R, first find out the path of R by using `which R`. It gives a path like `/your_R_path/bin/R`. I then specify `R_HOME` with the path to the bin directory: `R_HOME=/your_R_path/bin/`. Then go back to python and install. It works for me. – Pengyao Jul 06 '20 at 21:34
  • No commands given in the answer. Why so? –  Oct 29 '21 at 15:10
  • Interesting that this is not documented somewhere on the `rpy2` documentation page. I agree, this should be added! – Johannes Wiesner Feb 11 '22 at 12:01
5

With help from the comment above, I got moving in the proper direction. I created the R_HOME as a SYSTEM variable. That didn't work right away, as I was getting an R_USER not defined error.

To get everything the import above to work without error, I simply re-installed the rpy2 exectuable from here after defining the R_HOME. No errors.

In summary, key was o have R_HOME set PRIOR TO installing rpy2.

Btibert3
  • 38,798
  • 44
  • 129
  • 168
  • This seems odd. I remember that setting environment variables in Windows required to log-out/log-in (user-level variable) and reboot (system-level variable). May be this was the issue ? – lgautier Jul 12 '13 at 13:51