Rpy2找不到R.dll [英] Rpy2 Cannot Find R.dll

查看:491
本文介绍了Rpy2找不到R.dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Python 2.7.9的Windows 7 64位系统上,已经安装了R-3.2.0(以前也尝试过R-2.12.2和R-3.1.3,但得到了相同的结果),在<系统路径,添加R_HOME作为值为C:\Program Files\R\R-3.2.0的系统变量,添加R_USER值为"Matt",并安装带有.exe文件的rpy2-2.3.0dev.

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.

但是,键入from rpy2 import robjects会在rpy2 \ rinterface \ __ init__.py中产生以下错误:

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 ##

进入rinterface \ __ init__.py,但这什么也没做.

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

我还尝试将所有内容从bin \ i386文件夹复制到bin文件夹,但仍然没有任何作用.

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

如何获取rpy2来查找r.dll?

How do I get rpy2 to find r.dll?

推荐答案

我对此问题进行了多次尝试,发现可能不是那么优雅,但是对于整个问题而言,一个简单的解决方法无法导入rpy2故事.如果您也厌倦了使用env变量,则只需在一个小的python脚本中进行设置即可,该脚本可以在开始时导入. 首先获取rpy2 .whl软件包,然后根据 https://stackoverflow.com/a/32983656/6912069 进行安装 然后只需创建一个小的python脚本,然后将其导入依赖于rpy2的其他python脚本的开头. 对我来说,这是可行的:

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

在我的主要python脚本的开头导入此脚本时,我可以使用rpy2包并从Python内部控制R.

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

这篇关于Rpy2找不到R.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆