简体   繁体   English

rpy2无法导入“ rgl” R包

[英]rpy2 fails to import 'rgl' R package

To load the rgl package check if the rgl package is installed in R from within python , I'm combining the recipes given in these two questions: 加载rgl软件包,请检查rgl软件包是否已从python安装在R ,我将结合以下两个问题中给出的配方:

rpy2: check if package is installed rpy2:检查软件包是否已安装

rpy2 importr failing with xts and quantmod rpy2导入器因xts和Quantmod失败

Here's the MWE I put together: 这是我汇总的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)

The above code keeps failing with the error: 上面的代码不断失败,并显示以下错误:

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)

According to the answer given in the second question linked at the beginning of this question, the line: 根据此问题开头链接的第二个问题给出的答案,该行如下:

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

should take care of this error, but apparently it is not. 应该注意这个错误,但是显然不是。

What am I doing wrong here? 我在这里做错了什么?


Edit 编辑

A comment below by Spacedman made me realize there was a typo in the original question above ( if packname == 'rlg' should be if packname == 'rgl' ). Spacedman在下面的评论使我意识到上面的原始问题有错别字( if packname == 'rlg'应该是if packname == 'rgl' )。 Correcting this typo, I was able to make the code work, as follows: 更正了这种错字,我可以使代码正常工作,如下所示:

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)

So it actually works, but it's terribly cumbersome and ugly. 因此它确实有效,但是却非常麻烦和丑陋。 I suggest using the code I posted in my answer below which is a far better way to check for installed packages. 我建议使用我在下面的答案中发布的代码,这是检查已安装软件包的更好的方法。

Thanks Spacedman! 谢谢Spacedman!

You can't do that. 你不能那样做。 The rgl package needs an OpenGL device, but running in the confines of rpy2 is more like running headless. rgl软件包需要OpenGL设备,但是在rpy2范围内运行更像是无头运行。

You are almost certainly not fixing all symbols creating an error. 几乎可以肯定,您并没有修复所有创建错误的符号。 In the case of rgl , there are quite a few so may consider building the dictionary using an alternative name translation rule. rgl ,有很多,因此可以考虑使用替代名称转换规则来构建字典。

For example, changing '.' -> '_' 例如,更改'.' -> '_' '.' -> '_' to '.' -> '__' '.' -> '_''.' -> '__' '.' -> '__' : '.' -> '__'

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)

I'm marking Dirk's answer as accepted because he was right. 我将Dirk的答案标记为已接受,因为他是对的。 Nonetheless, I'm posting my own answer here to show how I solved this issue. 尽管如此,我还是在这里发布自己的答案,以说明如何解决此问题。

Here's the code: 这是代码:

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

This will return (stored in rpack ) a list of all the available packages in R . 这将返回(存储在rpackR中所有可用软件包的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM