rpy2无法加载外部库

时间:2013-09-11 15:21:32

标签: python r rpy2

希望有人可以帮忙解决这个问题。

R版本:2.14.1
rpy2版本:2.2.5
python版本:2.7.3

尝试在python脚本中使用rpy2加载R venneuler包,它具有rJava作为依赖项。 venneuler和rJava都成功地从R环境中加载。其他R库,例如'stats','car'等,可以使用rpy2中的importr在python中加载。

Python代码:

 >from rpy2 import robjects  
 >from rpy2.robjects.packages import importr  
 >venn=importr('venneuler')

出现以下错误:

Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘venneuler’
Error in .Primitive("as.environment")("package:venneuler") : 
  no item called "package:venneuler" on the search list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/packages.py", line 117, in importr
    env = _as_env(rinterface.StrSexpVector(['package:'+name, ]))
rpy2.rinterface.RRuntimeError: Error in .Primitive("as.environment")("package:venneuler") : 
  no item called "package:venneuler" on the search list

在故障排除过程中,我尝试使用importr直接加载rJava:

>java=importr('rJava')

得到以下错误,

Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/home/adam/R/x86_64-pc-linux-gnu-library/2.14/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory
Error in .Primitive("as.environment")("package:rJava") : 
  no item called "package:rJava" on the search list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/packages.py", line 117, in importr
    env = _as_env(rinterface.StrSexpVector(['package:'+name, ]))
rpy2.rinterface.RRuntimeError: Error in .Primitive("as.environment")("package:rJava") : 
  no item called "package:rJava" on the search list

当我检查'/home/adam/R/x86_64-pc-linux-gnu-library/2.14/rJava/libs/rJava.so'它存在并且似乎正确链接到java动态库(例如ldd rJava 。所以)。

有关如何解决此问题的任何想法?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,并通过Stack Overflow寻找了几个答案。在这里,我终于找到了答案:

error: unable to load installed packages just now

尝试运行

R CMD javareconf -e

没有sudo或任何东西,就像普通用户一样,然后尝试加载库。

让我知道它是否有效。它确实适合我。

干杯!

答案 1 :(得分:0)

我敢打赌,通过以下尝试,您的错误将消失:

from rpy2.robjects.packages import importr
utils = importr('utils')
utils.install_packages('rJava')

现在您可以在python中编译r代码,但是请确保通过上述尝试安装R依赖项。希望这对您有用。