尝试在Windows xp下使用Python 2.6,使Rpy2 2.1.9与R 2.12.1配合工作时出现奇怪的问题 - Rpy无法找到R.dll?

8
我一直在尝试让Rpy2与我的R安装良好配合,但一直存在问题。我首先尝试安装rpy2 MSI软件包,但似乎并没有成功。当我运行推荐的测试时,出现了一个错误,说找不到R.dll,因为新的R安装(2.11之后)将DLL文件安装到i386文件夹中,而rpy2无法在bin文件夹中找到它们,因为它正在查找bin/i386文件夹。

然后我尝试使用python自带的命令行工具(distutils)从头构建安装程序。但这并没有成功,因为setup.py声称无法找到R_home位置。但我发现编辑环境变量(PATH)可能会向rpy2设置显示R安装位置。然后我对环境进行了几处编辑,添加了指向bin/i386目录的“R_home”变量,并在PATH变量下创建了一个新条目,指向同一位置。

不幸的是,当它找到R路径时,我遇到了以下问题:

running build
running build_py
running build_ext
Traceback (most recent call last):
  File "setup.py", line 372, in <module>
    [os.path.join('doc', 'source', 'rpy2_logo.png')])]
  File "C:\Python26\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands
    self.run_command(cmd)
  File "C:\Python26\lib\distutils\dist.py", line 995, in run_command
    cmd_obj.run()
  File "C:\Python26\lib\distutils\command\build.py", line 134, in run
    self.run_command(cmd_name)
  File "C:\Python26\lib\distutils\cmd.py", line 333, in run_command
    self.distribution.run_command(command)
  File "C:\Python26\lib\distutils\dist.py", line 994, in run_command
    cmd_obj.ensure_finalized()
  File "C:\Python26\lib\distutils\cmd.py", line 117, in ensure_finalized
    self.finalize_options()
  File "setup.py", line 111, in finalize_options
    config += get_rconfig(r_home, about)
  File "setup.py", line 264, in get_rconfig
    rc = RConfig.from_string(rconfig)
  File "setup.py", line 252, in from_string
    + '\nin string\n' + string)
ValueError: Invalid substring in string

所以我又尝试使用预制安装,以为新的环境编辑可能有用,但遇到了这个问题

 Traceback (most recent call last):
      File "<string>", line 245, in run_nodebug
      File "C:\Documents and Settings\User\Desktop\rpy2-2.1.9\rpy\tests.py", line 3, in <module>
        import rpy2.robjects.tests
      File "C:\Python26\lib\site-packages\rpy2\robjects\__init__.py", line 12, in <module>
        import rpy2.rinterface as rinterface
      File "C:\Python26\lib\site-packages\rpy2\rinterface\__init__.py", line 56, 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-2.12.1\bin\i386

这真的很奇怪,因为(任何人都可以在他们自己的安装中检查)R将R.dll安装到“C:\Program Files\R\R-2.12.1\bin\i386”中,我已经检查并验证了它是否存在,并且我已经将rpy2指向了Windows默认路径中的此目录!我确切知道rpy2正在查找正确的位置,但无法理解为什么它看不到R.dll。
那么为什么rpy2找不到它呢?有没有人知道如何让rpy2与R 2.12一起工作?也许我应该尝试更新的rpy2 2.2.0版本?尽管它仍在开发中,但根据这个网站,1.9应该能够处理R 2.12,所以我不知道该怎么办...
感谢任何能帮忙的人...
[编辑]我也尝试了这里的这些说明,但它们返回相同的“找不到DLL”错误...除非你将环境变量“R_home”更改为直接指向c/program files/R/R 2.12目录而不是进入i386子目录。
如果它指向正确的位置,你会得到这些错误反馈。这看起来更有希望...但仍然很糟糕!
.......................F....................................F.FFF...F....................................................................F..................................
======================================================================
FAIL: testNewWithoutInit (rpy2.rinterface.tests.test_SexpVector.SexpVectorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_SexpVector.py", line 43, in testNewWithoutInit
    self.assertTrue(False) # worked when tested, but calling endEmbeddedR causes trouble
AssertionError

======================================================================
FAIL: testCallErrorWhenEndedR (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 122, in testCallErrorWhenEndedR
    self.assertTrue(False) # worked when tested, but calling endEmbeddedR causes trouble
AssertionError

======================================================================
FAIL: testReadConsoleWithError (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 117, in testReadConsoleWithError
    self.assertTrue(errorstring.startswith('Traceback'))
AssertionError

======================================================================
FAIL: testSetReadConsole (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 97, in testSetReadConsole
    self.assertEquals(yes.strip(), res[0])
AssertionError: 'yes' != ''

======================================================================
FAIL: testSetWriteConsole (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 36, in testSetWriteConsole
    self.assertEquals('[1] "3"\n', str.join('', buf))
AssertionError: '[1] "3"\n' != ''

======================================================================
FAIL: testWriteConsoleWithError (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 55, in testWriteConsoleWithError
    self.assertTrue(errorstring.startswith('Traceback'))
AssertionError

======================================================================
FAIL: testVectorUnicodeCharacter (rpy2.robjects.tests.testNumpyConversions.NumpyConversionsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\robjects\tests\testNumpyConversions.py", line 54, in testVectorUnicodeCharacter
    self.assertTrue(False) # arrays of unicode characters causing segfault
AssertionError

----------------------------------------------------------------------
Ran 172 tests in 0.407s

FAILED (failures=7)
Exit code:  True
3个回答

19

在网上搜寻并尝试了许多不同的方法之后,终于在我的Windows 7电脑上使Rpy2工作了很多小时!

基本上,关键帮助来自于这个线程:http://www.mail-archive.com/rpy-list@lists.sourceforge.net/msg03348.html

总结一下,在Windows7上启动rpy2的步骤如下:

  1. 从此链接安装rpy2:https://bitbucket.org/breisfeld/rpy2_w32_fix/issue/1/binary-installer-for-win32
  2. 将R.dll的路径添加到环境变量PATH中(在我的情况下是C:\Program Files\R\R-2.12.1\bin\i386)
  3. 添加一个环境变量R_HOME(在我的情况下是C:\Program Files\R\R-2.12.1)
  4. 添加一个环境变量R_USER(就是Windows中的用户名)
  5. 重新启动Python IDE(否则环境变量不会被重新加载!)

1
如果您不知道如何添加/更改环境变量,请参考此处:http://www.computerhope.com/issues/ch000549.htm。 - kadee
1
这是一个很棒的解决方案。WIN7在设置环境变量后,在测试调用R之前注销并重新登录有所帮助。此外,这也适用于64位机器上的rpy2。Win安装程序在这里http://www.lfd.uci.edu/~gohlke/pythonlibs/。 - pythOnometrist
太棒了!适用于R-3.1.0,Windows 7 64位,Python 2.7.6。 - pbreach
这个解决方案在我的环境下不起作用,我的环境是 R 3.0.1,Enthought Python 2.7.3 64 位,Win 7 64 位。 - fileunderwater

4

RPy2在Windows上没有进行测试。你可以尝试使用一个带有Windows安装程序的旧版本(2.0.8),但这可能会在新版本的R上出现问题。

作者不使用Windows。如果您知道如何让较新版本在Windows上运行,我相信他会欢迎您的贡献。


我希望我有,但不幸的是我完全没有编程知识!实际上,我曾试图使用rpy和R来学习一些编程技能... - Kingdom For a Name
@王国: 好的,我问过Laurent今天早些时候,他说可能需要几个小时的工作。但显然他有了一台新的Windows机器,所以请密切关注2.2版本。 - Thomas K
啊,那太好了!我只需要继续使用当前版本,并尝试解决任何出现的问题。 - Kingdom For a Name
有更新了吗?我真的很想在Windows上使用最新版本。 - Jordan
好的,谢谢回复。如果我有时间,我会处理这件事,但是有点怀疑。我可能最终会使用R.NET或rsruby之类的东西(我不太在意使用什么语言——对于当前任务来说,生产力是相似的)。 - Jordan
显示剩余3条评论

3

我不确定在哪里写这个,因为我不能评论(没有声望积分),但是我认为这是关于这个问题有用的信息。即使您知道R.dll位于指定位置,也会出现令人恼火的“无法找到R.dll”的错误消息,原因是程序实际上并没有在该目录中查找。我认为相关操作发生在rpy2的“init.py”模块的以下行中:

import win32api
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', _win_bindir)
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', _win_bindir)
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')

# Load the R dll using the explicit path
R_DLL_DIRS = ('bin', 'lib')
# Try dirs from R_DLL_DIRS
for r_dir in R_DLL_DIRS:
    Rlib = os.path.join(R_HOME, r_dir, _win_bindir, 'R.dll')
    if not os.path.exists(Rlib):
        continue
    win32api.LoadLibrary( Rlib )
    _win_ok = True
    break
# Otherwise fail out!
if not _win_ok:
    raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)

正如您所见,错误信息总是会说它正在查找您设置为R_HOME的任何目录,但实际上它将会在目录“R_HOME\ r_dir\win_bindir”中查找。因此,当您看到像“无法在C:\Program Files\R\R-2.12.1\bin\i386内定位R.dll”的消息时,那是因为它实际上在查找一个名为C:\Program Files\R\R-2.12.1\bin\i386\R-2.12.1\bin\i386之类的目录。

Kadee答案中的第三点通过仅保留路径指定到\bin级别的上方来修复此问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接