RPy2: LookupError when calling R function 'DEoptim' in python

286 views Asked by At

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.

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.

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

2

There are 2 answers

1
lgautier On

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

0
pbreach On

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")