rpy2 无法导入“rgl"R 包 [英] rpy2 fails to import 'rgl' R package

查看:32
本文介绍了rpy2 无法导入“rgl"R 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

rpy2:检查软件包是否已安装

rpy2 导入器因 xts 和 quantmod 而失败

这是我整理的 MWE:

Here's the MWE I put together:

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.

我在这里做错了什么?

编辑

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

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

感谢 Spacedman!

Thanks Spacedman!

推荐答案

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

You can't do that. The rgl package needs an OpenGL device, but running in the confines of rpy2 is more like running headless.

这篇关于rpy2 无法导入“rgl"R 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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