Reputation: 16987
I've done this before (~1 year ago now), but want to use R again via rpy2. I've confirmed that I can load default packages when I do this:
from rpy2 import robjects
plot = robjects.r.get('plot')
Now, when I try to load an external package like so,
from rpy2.robjects.packages import importr
betareg = importr('betareg')
I get the following error:
Traceback (most recent call last):
File "<ipython-input-1-b1ed20534ab9>", line 1, in <module>
runfile('C:/Users/Patrick/OneDrive/FIDS/betareg.py', wdir='C:/Users/Patrick/OneDrive/FIDS')
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Patrick/OneDrive/FIDS/betareg.py", line 11, in <module>
betareg = importr('betareg')
File "C:\Anaconda\lib\site-packages\rpy2\robjects\packages.py", line 438, in importr
env = _get_namespace(rname)
RRuntimeError: Error in loadNamespace(name) : there is no package called 'betareg'
I have R_HOME
, R_USER
, and R_LIBS
system variables set to make sure R gets loaded and (hopefully) gets pointed to where the external packages are located. The same thing also happens when I set the lib_loc
kwarg in importr
as the path to the package folder. I can load the package via library('betareg')
in R from command line with the same run arguments that r2py is using (--quiet, --no-save).
Am I missing anything here? How can I get set up so that I can load installed external packages?
OS:
Microsoft Windows [Version 10.0.10240]
Python version:
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52)
[MSC v.1500 64 bit (AMD64)] on win32
R version:
R version 3.2.0 (2015-04-16) -- "Full of Ingredients"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
rpy2 version:
>>>import rpy2
>>> rpy2.__version__
'2.6.3'
Upvotes: 2
Views: 4180
Reputation: 126
To use external R packages in Conda you will install these packages in Conda. If you install them from R you can't use them. To install (e.g. for dtw library) it do next like:
import rpy2.robjects.packages as r
utils = r.importr("utils")
package_name = "dtw"
utils.install_packages(package_name)
Output is:
rpy2.rinterface.NULL
Then you can use this package
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()
# Set up our R namespaces
R = rpy2.robjects.r
DTW = importr('dtw') # our package
Upvotes: 7