rpy2未能导入' rgl' R包

时间:2015-02-24 02:43:23

标签: python r rpy2

加载rgl软件包,检查rglR是否安装python from rpy2.rinterface import RRuntimeError from rpy2.robjects.packages import importr utils = importr('utils') def importr_tryhard(packname, contriburl): try: if packname == 'rlg': rgl = importr("rgl", robject_translations = {".rgl.abclines": "_rgl_abclines2"}) else: rpack = importr(packname) print 'success' except RRuntimeError: print 'no pack' rpack = 'none' return rpack packname = 'rgl' contriburl = 'http://cran.stat.ucla.edu/' importr_tryhard(packname, contriburl) ,我是否合并rpy2.robjects.packages.LibraryError: Conflict when converting R symbol in the package "rgl" to a Python symbol (rgl.abclines -> rgl_abclines while there is already rgl_abclines) 这两个问题中提供的食谱:

rpy2: check if package is installed

rpy2 importr failing with xts and quantmod

这是我放在一起的MWE:

rgl = importr("rgl", robject_translations = {".rgl.abclines": "_rgl_abclines2"})

以上代码因错误而失败:

if packname == 'rlg'

根据在本问题开头链接的第二个问题中给出的答案,该行:

if packname == 'rgl'

应该处理这个错误,但显然不是。

我在这里做错了什么?


修改

Spacedman在下面的评论让我意识到上面的原始问题中有一个拼写错误(from rpy2.rinterface import RRuntimeError from rpy2.robjects.packages import importr utils = importr('utils') def importr_tryhard(packname, contriburl): try: if packname == 'rgl': rpack = importr("rgl", robject_translations = {"rgl.abclines": "rgl_abclines2", "rgl.attrib": "rgl_attrib2", "rgl.attrib.count": "rgl_attrib_count2", "rgl.bbox": "rgl_bbox2", "rgl.bg": "rgl_bg2", "rgl.clear": "rgl_clear2", "rgl.dev.list": "rgl_dev_list2", "rgl.getcolorcount": "rgl_getcolorcount2", "rgl.getmaterial": "rgl_getmaterial2", "rgl.ids": "rgl_ids2", "rgl.init": "rgl_init2", "rgl.light": "rgl_light2", "rgl.material": "rgl_material2", "rgl.pixels": "rgl_pixels2", "rgl.planes": "rgl_planes2", "rgl.pop": "rgl_pop2", "rgl.postscript": "rgl_postscript2", "rgl.primitive": "rgl_primitive2", "rgl.quit": "rgl_quit2", "rgl.selectstate": "rgl_selectstate2", "rgl.setMouseCallbacks": "rgl_setMouseCallbacks2", "rgl.setselectstate": "rgl_setselectstate2", "rgl.snapshot": "rgl_snapshot2", "rgl.spheres": "rgl_spheres2", "rgl.sprites": "rgl_sprites2", "rgl.surface": "rgl_surface2", "rgl.texts": "rgl_texts2", "rgl.user2window": "rgl_user2window2", "rgl.viewpoint": "rgl_viewpoint2", "rgl.window2user": "rgl_window2user2"}) else: rpack = importr(packname) print 'success' except RRuntimeError: print 'no pack' rpack = 'none' return rpack packname = 'rgl' contriburl = 'http://cran.stat.ucla.edu/' importr_tryhard(packname, contriburl) 应该是{{1}})。纠正这个错误,我能够使代码工作,如下:

{{1}}

所以它确实有效,但它非常麻烦和丑陋。我建议使用我在my answer below中发布的代码,这是检查已安装软件包的更好方法。

谢谢Spacedman!

3 个答案:

答案 0 :(得分:1)

你不能这样做。 rgl包需要一个OpenGL设备,但在rpy2的范围内运行更像是无头运行。

答案 1 :(得分:1)

您几乎肯定不会修复所有创建错误的符号。在rgl的情况下,有很多人可以考虑使用替代名称转换规则来构建字典。

例如,将'.' -> '_'更改为'.' -> '__'

from rpy2.robjects.packages import importr
base = importr('base')
base.library('rgl')
env = base.asNamespace('rgl')
d = dict()
for k in env:
    if '.' in k:
        new_k = k.replace('.',  '__')
        d[k] = new_k
rgl = importr("rgl", robject_translations=d)

答案 2 :(得分:0)

我认为Dirk的回答是正确的,因为他是对的。尽管如此,我在这里发布了自己的答案,以说明我是如何解决这个问题的。

以下是代码:

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

def importr_tryhard():
    contriburl = 'http://cran.stat.ucla.edu/'   
    try:
        pack_lst = utils.installed_packages()
        rpack = list(pack_lst.rx(True, 1))
    except RRuntimeError:
        rpack = []
    return rpack

rpack = importr_tryhard()
print rpack

这将返回(存储在rpackR中所有可用包的列表。