4

I am on Windows 7 64bit with Python 2.7.9, have installed R-3.2.0 (also previously tried R-2.12.2 and R-3.1.3 but got the same result), added C:\Program Files\R\R-3.2.0\bin\i386 to the system path, added R_HOME as a system variable with value C:\Program Files\R\R-3.2.0, added R_USER with value "Matt", and installed rpy2-2.3.0dev with a .exe file, which installed.

However, typing from rpy2 import robjects gives the following error in rpy2\rinterface\__init__.py:

Traceback (most recent call last):
  File "C:\Python27\rpy2Test.py", line 2, in <module>
    from rpy2 import robjects
  File "C:\Python27\lib\site-packages\rpy2\robjects\__init__.py", line 14, in <module>
    import rpy2.rinterface as rinterface
  File "C:\Python27\lib\site-packages\rpy2\rinterface\__init__.py", line 79, in <module>
    raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)
RuntimeError: Unable to locate R.dll within C:Program Files\R\R-3.2.0

I have tried copying and pasting this:

if os.path.exists(os.path.join(R_HOME, 'lib')):             ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin')    ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules')    ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')    ## ADDED ##
else:                                   ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', 'i386')     ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', 'i386') ## ADDED ##
    os.environ['PATH'] += ';' + os.path.join(R_HOME, 'library')     ## ADDED ##

# Load the R dll using the explicit path
# First try the bin dir:
Rlib = os.path.join(R_HOME, 'bin', 'R.dll')
# Try bin/i386 subdirectory seen in R 2.12.0                ## ADDED ##
if not os.path.exists(Rlib):                        ## ADDED ##
    Rlib = os.path.join(R_HOME, 'bin', 'i386', 'R.dll')         ## ADDED ##

into rinterface\__init__.py, but this did nothing.

I have also tried copying everything from the bin\i386 folder into the bin folder, but still makes no difference.

How do I get rpy2 to find r.dll?

Matt Majic
  • 381
  • 9
  • 18
  • 3
    Does this answer your question? [OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\R.dll': error 0x7e](https://stackoverflow.com/questions/63863449/oserror-cannot-load-library-c-program-files-r-r-4-0-2-bin-x64-r-dll-error-0) – Bruno Gabuzomeu Sep 25 '20 at 07:30
  • Sorry I have no idea, I can't remember the question. Should I take it down? – Matt Majic Sep 25 '20 at 13:18

2 Answers2

2

I struggled with this problem multiple times and found a maybe not so elegant, but rather simple workaround to this whole can't import rpy2 story. If you are also tired of messing with env variables, then just set it in a small python script which you can import at the beginning. First get the rpy2 .whl package and install it according to https://stackoverflow.com/a/32983656/6912069 Then just creat a small python script which you import at the beginning of your other python scripts which depend on rpy2. For me this worked out:

from __main__import *
import os
os.environ['PYTHONHOME'] = 'C:/Program Files/Python'
os.environ['PYTHONPATH'] = 'C:/Program Files/Python/lib/site-packages'
os.environ['R_HOME'] = 'C:/Program Files/R/R-3.5.1'
os.environ['R_USER'] = 'C:/Program Files/Python/Lib/site-packages/rpy2'

# importing rpy2 now throws no errors
import rpy2.robjects as ro

When importing this script at the beginning of my main python script, I can use the rpy2 package and control R from within Python.

N. Maks
  • 539
  • 3
  • 15
1

Last night I made an rpy2 install pdf which addresses this issue -- either R, Pyhton, pywin32 or rpy2 linked in the PATH are most likely not 32-bit.

Maybe it will help - rpy2pandas.pdf

(Only thing is I have Python in C:/Python27/ArcGIS10.2/python.exe, otherwise everything else should port.)

Jason Matney
  • 552
  • 6
  • 24
  • 1
    Thanks for answering, however I decided to use PypeR instead. Anyway I tried reinstalling rpy2 with pip and the .whl file, and I installed singledispatch, but the problem is still there. – Matt Majic May 02 '15 at 09:37
  • Wow! I was using R 64 bit. Shame on me. I never expected this would be a problem. But thank you for solving this. – BAMF4bacon Nov 12 '15 at 18:09