RPy2:在python中调用R函数'DEoptim'时的LookupError

时间:2021-03-16 23:16:26

I've finally got RPy2 working on my Windows 7 computer with Python 2.7.8 and R 3.10.1. I want to call the R function 'DEoptim' which did not come with my R installation however the package has been downloaded via the R Repository and is working in R.

我终于使用Python 2.7.8和R 3.10.1在我的Windows 7计算机上运行了RPy2。我想调用R函数'DEoptim',它不是我的R安装,但是该软件包已经通过R Repository下载并在R中工作。

When I do this:

我这样做的时候:

import rpy2.robjects as robjects
dea = robjects.r['DEoptim']

I get the following error:

我收到以下错误:

LookupError                               Traceback (most recent call last)
<ipython-input-3-a882c24e8623> in <module>()
----> 1 dea = robjects.r['DEoptim']

C:\Users\Patrick\Anaconda\lib\site-packages\rpy2\robjects\__init__.pyc in __getitem__(self, item)
    224 
    225     def __getitem__(self, item):
--> 226         res = _globalenv.get(item)
    227         res = conversion.ri2ro(res)
    228         res.__rname__ = item

LookupError: 'DEoptim' not found

Which seems to make sense. Python is trying to find this package however it is not there. When importing this package in R I can see this that it is located at: C:/Users/Patrick/Documents/R/win-library/3.1.

这似乎有道理。 Python试图找到这个包,但它不存在。在R中导入此包时,我可以看到它位于:C:/Users/Patrick/Documents/R/win-library/3.1。

Is there any way that I can call this function from python? I've looked around for a good DE optimization package in python and found insyred however using the DEoptim package from R is much easier. Also, there are a lot of other R packages not in the standard library that would be great to have around once in a while

有什么办法可以从python中调用这个函数吗?我在python中寻找了一个很好的DE优化包,然后发现了insyred然而使用R的DEoptim包更加容易。此外,标准库中还有很多其他R包,偶尔会有很多

2 个解决方案

#1


1  

You will likely need to load the relevant R package. The rpy2 function importr provide a simple interface to R packages.

您可能需要加载相关的R包。 rpy2函数导入器为R包提供了一个简单的接口。

#2


0  

This is the code that solved the problem for me in case anyone gets stuck or doesn't take enough time to read the documentation (like me..)

这是为我解决问题的代码,以防任何人卡住或没有足够的时间阅读文档(像我...)

from rpy2.robjects.packages import importr
dea = importr('DEoptim', lib_loc="C:/Users/Patrick/Documents/R/win-library/3.1")

#1


1  

You will likely need to load the relevant R package. The rpy2 function importr provide a simple interface to R packages.

您可能需要加载相关的R包。 rpy2函数导入器为R包提供了一个简单的接口。

#2


0  

This is the code that solved the problem for me in case anyone gets stuck or doesn't take enough time to read the documentation (like me..)

这是为我解决问题的代码,以防任何人卡住或没有足够的时间阅读文档(像我...)

from rpy2.robjects.packages import importr
dea = importr('DEoptim', lib_loc="C:/Users/Patrick/Documents/R/win-library/3.1")