在 OSX 上安装 rpy2 以与 R 3.4.0 一起使用

Installing rpy2 to work with R 3.4.0 on OSX

我想使用一些需要 R 3.4 及以上版本的 R 包。我想通过 rpy2 (2.8) 在 python (3.6.1) 中访问这些包。

我安装了 R 3.4 版,它位于 /Library/Frameworks/R.framework/Resources 但是,当我使用 pip3 install rpy2 安装和使用 python 3.6.1 时 /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6) 作为我的翻译,我得到错误:

Traceback (most recent call last): File "/Users/vincentliu/PycharmProjects/magic/rpy2tester.py", line 1, in from rpy2 import robjects File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/robjects/init.py", line 16, in import rpy2.rinterface as rinterface File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/rinterface/init.py", line 92, in from rpy2.rinterface._rinterface import (baseenv, ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so, 2): Library not loaded: @rpath/libiconv.2.dylib Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so Reason: Incompatible library version: _rinterface.cpython-36m-darwin.so requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0

起初看起来像是 Anaconda 引起的问题,所以我删除了所有与 Anaconda 相关的文件,但问题仍然存在。

然后我卸载了rpy2,重新安装了Anaconda并使用conda install rpy2安装,这也通过Anaconda安装了R version 3.3.2。然后我可以将解释器更改为 /anaconda/bin/python 并可以正常使用 rpy2,但我无法使用我关心的 R 包,因为它们需要 R 3.4 及更高版本。显然,Anaconda 可以安装的最旧版本是 3.3.2,那么有什么方法可以将 rpy2 与 R 版本 3.4 一起使用?

我可以看到这个问题的两个通用解决方案。一种是通过 conda 安装 rpy2,然后以某种方式将其依赖的 R 更改为系统中的 3.4。另一种解决方法是解决错误

Incompatible library version: _rinterface.cpython-36m-darwin.so requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0

经过多次努力,我都没有找到好的结果。

我卸载了 rpy2 并使用 --verborse 重新安装。然后我发现

ld: warning: ignoring file /opt/local/lib/libpcre.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libpcre.dylib ld: warning: ignoring file /opt/local/lib/liblzma.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/liblzma.dylib ld: warning: ignoring file /opt/local/lib/libbz2.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libbz2.dylib ld: warning: ignoring file /opt/local/lib/libz.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libz.dylib ld: warning: ignoring file /opt/local/lib/libiconv.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libiconv.dylib ld: warning: ignoring file /opt/local/lib/libicuuc.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libicuuc.dylib ld: warning: ignoring file /opt/local/lib/libicui18n.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/libicui18n.dylib ld: warning: ignoring file /opt/local/Library/Frameworks/R.framework/R, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/Library/Frameworks/R.framework/R

所以我认为原因是 opt/local 中的 libiconv 的体系结构不兼容,导致 make 退回到 usr/lib 中过时的 libiconv。这很奇怪,因为我的机器应该是 运行 on x86_64 而不是 i386。然后我尝试 export ARCHFLAGS="-arch x86_64" 并使用端口重新安装 libiconv。这解决了问题。

我在下载 https://bitbucket.org/rpy2/rpy2/downloads/ 时卸载了安装的 pip 版本并从源 python setup.py install 安装。 FWIW 也根本不使用 Anaconda。