rpy2无法使用rJava-mac导入R包

时间:2016-09-08 10:49:52

标签: python r rpy2 rjava

我想使用rpy2 import R package' iqspr'我已经安装并测试了我的Rstudio,这个包工作正常。

以下是我遇到的错误。

from rpy2.robjects.packages import importr

java=importr('rJava')
iqspr=importr('iqspr')

错误

/Library/Python/2.7/site-packages/rpy2-2.8.3-py2.7-macosx-10.11-intel.egg/rpy2/rinterface/__init__.py:185: RRuntimeWarning: Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so':
  dlopen(/usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so
  Reason: image not found

  warnings.warn(x, RRuntimeWarning)
/Library/Python/2.7/site-packages/rpy2-2.8.3-py2.7-macosx-10.11-intel.egg/rpy2/rinterface/__init__.py:185: RRuntimeWarning: Error: .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so':
  dlopen(/usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so
  Reason: image not found

  warnings.warn(x, RRuntimeWarning)
Traceback (most recent call last):
  File "/Users/yang/Desktop/Chemical compounds design important materials/chemical_compound_design.py", line 4, in <module>
    java=importr('rJava')
  File "/Library/Python/2.7/site-packages/rpy2-2.8.3-py2.7-macosx-10.11-intel.egg/rpy2/robjects/packages.py", line 453, in importr
    env = _get_namespace(rname)
rpy2.rinterface.RRuntimeError: Error: .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so':
  dlopen(/usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so
  Reason: image not found

1 个答案:

答案 0 :(得分:0)

过去曾报告涉及RStudio和rJava的问题(例如rJava load error in RStudio/R after "upgrading" to OSX Yosemite),原因很可能类似。

尝试

export LD_LIBRARY_PATH=/usr/local/lib/R/3.3/site-library/rJava/libs/:{LD_LIBRARY_PATH}

在开始Python之前。