8

I've been having a real issue trying to make Rpy2 play nice with my R install. I first tried installing the rpy2 MSI package, and this didn't appear to work. When I ran the recommended tests, it was giving me an error saying that it couldn't find the R.dll, because the new R installs (post 2.11) install the DLLs into an i386 folder, where rpy2 can't find them because its looking in the bin folder instead of the bin/i386 folder.

Then I tried to build the install from scratch myself using the command line tools (distutils) included with python. This didn't work, because setup.py claimed to be unable to find the R_home location. But I did work out that editing an environment variable (PATH) might show the rpy2 setup where to find the R installation. I then made a couple of edits to the environment, adding the "R_home" variable pointing to the bin/i386 directory, and made a new entry under the PATH variable, pointing to the same spot.

Unfortunately, when it found the R path, I got this issue instead:

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

So I went back to trying to use the premade install, thinking that maybe the new edits to the environment might work but got this issue here

 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

This is REALLY weird, because (as anyone can check on their own install) R installs R.dll into "C:\Program Files\R\R-2.12.1\bin\i386" and I've checked and verified that its in there, and I've pointed rpy2 to this directory in the windows default PATH! I know for a fact that rpy2 is looking in the right place, but can't understand why its not seeing R.dll.

So why can't rpy2 find it? And does anyone know a way to get rpy2 working with R 2.12? Perhaps I should try the newer rpy2 2.2.0 version? Its still in development though, and 1.9 is supposed to be able to handle R 2.12 according to this website so I don't know what to do...

Thanks to anyone who can help out...

[EDIT] I've also tried these instructions over here but they return the same "can't find DLL" error... Unless you change the environment variable "R_home" to point straight at the c/program files/R/R 2.12 directory instead of into the i386 subdirectory.

If it points at the right place, you get these errors back. This looks a bit more promising... But its still pretty bad!

.......................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
4

3 回答 3

19

在网上搜索了几个小时并尝试了许多不同的东西,其中遇到了与上述相同的问题,我终于让 Rpy2 在我的 Windows 7 计算机上运行!

基本上,关键的帮助来自这个线程: 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(否则不会重新加载环境变量!)
于 2012-08-26T13:09:59.433 回答
4

RPy2 未在 Windows 上测试。您可以尝试将旧版本(2.0.8) 与 Windows 安装程序一起使用,但新版本的 R 可能会出现问题。

作者不使用Windows。如果您具备在 Windows 上运行更新版本的知识,我相信他会欢迎您的贡献。

于 2011-02-21T17:36:44.510 回答
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 回答中的第 3 点通过将指定的路径仅保留到 \bin 级别之上来解决此问题。

于 2013-12-31T00:08:33.877 回答