将 R 函数输出读取为列 [英] Read R function output as columns
问题描述
我正在想办法解决我昨天问的这个问题:
我的目标是从 python
中检查某些软件包是否安装在 R
中.
遵循 Dirk Eddelbuettel 在对 他的回答,我正在使用 R
中的 installed.packages()
函数来列出所有可用的包.
这是我目前得到的:
from rpy2.rinterface import RRuntimeError从 rpy2.robjects.packages 导入导入器utils = importr('utils')def importr_tryhard(packname, contriburl):尝试:rpack = utils.installed_packages()除了 RRuntimeError:rpack = []返回 rpackcontriburl = 'http://cran.stat.ucla.edu/'rpack = importr_tryhard(packname, contriburl)打印包
返回一个相当大的表单输出:
包 LibPath 版本ks "ks" "/usr/local/lib/R/site-library" "1.8.13"misc3d "misc3d" "/usr/local/lib/R/site-library" "0.8-4"mvtnorm "mvtnorm" "/usr/local/lib/R/site-library" "0.9-9996"rgl "rgl" "/usr/local/lib/R/site-library" "0.93.986"基基"/usr/lib/R/library"3.0.1"引导引导"/usr/lib/R/library"1.3-9"类类"/usr/lib/R/library"7.3-9"集群集群"/usr/lib/R/library"1.14.4"codetools "codetools" "/usr/lib/R/library" "0.2-8"编译器编译器"/usr/lib/R/library"3.0.1"数据集数据集"/usr/lib/R/library"3.0.1"外国外国"/usr/lib/R/library"0.8-49"图形图形"/usr/lib/R/library"3.0.1"grDevices "grDevices" "/usr/lib/R/library" "3.0.1"网格网格"/usr/lib/R/library"3.0.1"KernSmooth "KernSmooth" "/usr/lib/R/library" "2.23-10"格子格子"/usr/lib/R/library"0.20-23"MASS "MASS" "/usr/lib/R/library" "7.3-29"矩阵矩阵"/usr/lib/R/library"1.0-14"方法方法"/usr/lib/R/library"3.0.1"mgcv "mgcv" "/usr/lib/R/library" "1.7-26"nlme "nlme" "/usr/lib/R/library" "3.1-111"nnet "nnet" "/usr/lib/R/library" "7.3-7"并行并行"/usr/lib/R/library"3.0.1"rpart "rpart" "/usr/lib/R/library" "4.1-3"空间空间"/usr/lib/R/library"7.3-6"样条样条"/usr/lib/R/library"3.0.1"统计统计"/usr/lib/R/library"3.0.1"stats4 "stats4" "/usr/lib/R/library" "3.0.1"生存生存"/usr/lib/R/library"2.37-4"tcltk "tcltk" "/usr/lib/R/library" "3.0.1"工具工具"/usr/lib/R/library"3.0.1"utils "utils" "/usr/lib/R/library" "3.0.1"优先事项不适用杂项3d NAmvtnorm NArgl NA基地基地"引导推荐"推荐"类集群推荐"...
我只需要提取安装包的名称,所以第一列或第二列对我来说就足够了.
我尝试使用 np.loadtxt()
、np.genfromtxt()
和 with open(rpack) as csvfile:
,但没有人能够返回一个列表/数组,其中列或行被正确分隔(它们实际上都因不同的错误而失败).
我怎么能以列的形式读取这个输出,或者更准确地说,在列表/数组中提取已安装包的名称?
rpack
在你的情况下是一个 rpy2.robjects.vectors.Matrix
对象.因此,您可以简单地使用 rpy2 类方法 .rx()
来提取列:
mylist = list(rpack.rx(True, 1))
试试看.
I'm trying to come up with a way to solve this question I asked yesterday:
rpy2 fails to import 'rgl' R package
My goal is to check if certain packages are installed inside R
from within python
.
Following the recommendation by Dirk Eddelbuettel given in a comment on his answer, I'm using the installed.packages()
function from R
to list all the available packages.
This is what I've got so far:
from rpy2.rinterface import RRuntimeError
from rpy2.robjects.packages import importr
utils = importr('utils')
def importr_tryhard(packname, contriburl):
try:
rpack = utils.installed_packages()
except RRuntimeError:
rpack = []
return rpack
contriburl = 'http://cran.stat.ucla.edu/'
rpack = importr_tryhard(packname, contriburl)
print rpack
Which returns a quite large output of the form:
Package LibPath Version
ks "ks" "/usr/local/lib/R/site-library" "1.8.13"
misc3d "misc3d" "/usr/local/lib/R/site-library" "0.8-4"
mvtnorm "mvtnorm" "/usr/local/lib/R/site-library" "0.9-9996"
rgl "rgl" "/usr/local/lib/R/site-library" "0.93.986"
base "base" "/usr/lib/R/library" "3.0.1"
boot "boot" "/usr/lib/R/library" "1.3-9"
class "class" "/usr/lib/R/library" "7.3-9"
cluster "cluster" "/usr/lib/R/library" "1.14.4"
codetools "codetools" "/usr/lib/R/library" "0.2-8"
compiler "compiler" "/usr/lib/R/library" "3.0.1"
datasets "datasets" "/usr/lib/R/library" "3.0.1"
foreign "foreign" "/usr/lib/R/library" "0.8-49"
graphics "graphics" "/usr/lib/R/library" "3.0.1"
grDevices "grDevices" "/usr/lib/R/library" "3.0.1"
grid "grid" "/usr/lib/R/library" "3.0.1"
KernSmooth "KernSmooth" "/usr/lib/R/library" "2.23-10"
lattice "lattice" "/usr/lib/R/library" "0.20-23"
MASS "MASS" "/usr/lib/R/library" "7.3-29"
Matrix "Matrix" "/usr/lib/R/library" "1.0-14"
methods "methods" "/usr/lib/R/library" "3.0.1"
mgcv "mgcv" "/usr/lib/R/library" "1.7-26"
nlme "nlme" "/usr/lib/R/library" "3.1-111"
nnet "nnet" "/usr/lib/R/library" "7.3-7"
parallel "parallel" "/usr/lib/R/library" "3.0.1"
rpart "rpart" "/usr/lib/R/library" "4.1-3"
spatial "spatial" "/usr/lib/R/library" "7.3-6"
splines "splines" "/usr/lib/R/library" "3.0.1"
stats "stats" "/usr/lib/R/library" "3.0.1"
stats4 "stats4" "/usr/lib/R/library" "3.0.1"
survival "survival" "/usr/lib/R/library" "2.37-4"
tcltk "tcltk" "/usr/lib/R/library" "3.0.1"
tools "tools" "/usr/lib/R/library" "3.0.1"
utils "utils" "/usr/lib/R/library" "3.0.1"
Priority
ks NA
misc3d NA
mvtnorm NA
rgl NA
base "base"
boot "recommended"
class "recommended"
cluster "recommended"
...
I need to extract just the names of the packages installed, so either the first or the second columns would be enough for me.
I've tried using np.loadtxt()
, np.genfromtxt()
and with open(rpack) as csvfile:
, but none was able to give back a list/array where either the columns or the rows was correctly separated (they all failed with different errors actually).
How could I read this output in column form, or more to the point, extract the names of the installed packages in a list/array?
rpack
in your case is an rpy2.robjects.vectors.Matrix
object. Therefore you can simply use rpy2 class method .rx()
to extract the column:
mylist = list(rpack.rx(True, 1))
Have a try.
这篇关于将 R 函数输出读取为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!