Rpy2 Cannot Find R.dll

梦想与她 提交于 2020-02-05 18:34:05

问题


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?


回答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.)




回答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.



来源:https://stackoverflow.com/questions/29863535/rpy2-cannot-find-r-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!