rpy2:检查软件包是否已安装 [英] rpy2: check if package is installed
本文介绍了rpy2:检查软件包是否已安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用rpy2
,我要检查是否安装了给定的软件包.如果是这样,我将其导入.如果没有,我先安装它.
Using rpy2
, I want to check if a given package is installed. If it is, I import it. If not, I install it first.
如何检查它是否已安装?
How do I check if it's installed?
from rpy2 import *
if not *my package is installed*:
rpy2.interactive as r
r.importr("utils")
package_name = "my_package"
r.packages.utils.install_packages(package_name)
myPackage = importr("my_package")
推荐答案
此处是在Python端执行此操作的函数
(请注意contriburl
,应将其设置为CRAN镜像,并且不会处理库安装失败的情况.)
Here is a function that'd do it on the Python side
(note the contriburl
, that should be set to a CRAN mirror, and that the case where installing the library is failing is not handled).
from rpy2.rinterface import RRuntimeError
from rpy2.robjects.packages import importr
utils = importr('utils')
def importr_tryhard(packname, contriburl):
try:
rpack = importr(packname)
except RRuntimeError:
utils.install_packages(packname, contriburl = contriburl)
rpack = importr(packname)
return rpack
这篇关于rpy2:检查软件包是否已安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文