I would like to use some R packages requiring R version 3.4 and above. I want to access these packages in python (3.6.1) through rpy2 (2.8).
I have R version 3.4 installed, and it is located in /Library/Frameworks/R.framework/Resources
However, when I use pip3 install rpy2
to install and use the python 3.6.1 in /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6)
as my interpreter, I get the error:
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
Which first seemed like a problem caused by Anaconda, and so I remove all Anaconda-related files but the problem persists.
I then uninstalled rpy2, reinstalled Anaconda and used conda install rpy2
to install, which also installs R version 3.3.2 through Anaconda. I can then change the interpreter to /anaconda/bin/python
and can use rpy2 fine, but I couldn't use the R packages I care about because they need R version 3.4 and higher. Apparently, the oldest version Anaconda can install is 3.3.2, so is there any way I can use rpy2 with R version 3.4?
I can see two general solutions to this problem. One is to install rpy2 through conda and then somehow change its depending R to the 3.4 one in the system. Another solution is to resolve the error
Incompatible library version: _rinterface.cpython-36m-darwin.so requires version 8.0.0 or later, but libiconv.2.dylib provides version 7.0.0
After much struggling, I've found no good result with either.
I uninstalled rpy2 and reinstalled with --verborse
. I then found
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
So I supposed the reason is the architecture incompatibility of the libiconv in opt/local
, causing make to fall back onto the outdate libiconv in usr/lib
. This is strange because my machine should be running on x86_64 not i386. I then tried export ARCHFLAGS="-arch x86_64"
and reinstalled libiconv using port. This resolved the problem.