rpy2 无法导入 'rgl' R 包

rpy2 fails to import 'rgl' R package

加载rgl检查rgl包是否安装在python中的R,我'结合这两个问题给出的食谱:

rpy2: check if package is installed

rpy2 importr failing with xts and quantmod

这是我整理的 MWE:

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)

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

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

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

我做错了什么?


编辑

Spacedman 下面的评论让我意识到上面的原始问题中有一个错字(if packname == 'rlg' 应该是 if packname == 'rgl')。更正此错字后,我能够使代码正常工作,如下所示:

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)

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

谢谢太空人!

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

我将德克的回答标记为已接受,因为他是对的。尽管如此,我还是在这里发布了我自己的答案,以展示我是如何解决这个问题的。

代码如下:

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

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

您几乎可以肯定没有修复所有导致错误的符号。在 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)