rpy2 找不到已安装的外部 R 包 [英] rpy2 cannot find installed external R packages

查看:58
本文介绍了rpy2 找不到已安装的外部 R 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前做过这个(现在大约一年前),但想通过 rpy2 再次使用 R.我已经确认我可以在执行此操作时加载默认包:

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

我收到以下错误:

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'

我设置了 R_HOMER_USERR_LIBS 系统变量以确保 R 被加载并且(希望)被指向外部包位于.当我将 importr 中的 lib_loc kwarg 设置为包文件夹的路径时,也会发生同样的事情.我可以从命令行通过 library('betareg') 加载包,使用与 r2py 使用相同的运行参数(--quiet,--no-save).

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?

操作系统:

Microsoft Windows [Version 10.0.10240]

Python 版本:

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 版本:

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 版本:

>>>import rpy2
>>> rpy2.__version__
'2.6.3'

推荐答案

要在 Conda 中使用外部 R 包,您需要在 Conda 中安装这些包.如果您从 R 安装它们,则无法使用它们.要安装(例如对于 dtw 库),它接下来会执行以下操作:

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)

输出为:

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

这篇关于rpy2 找不到已安装的外部 R 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆