我試圖想出一個辦法來解決這個問題,我昨天問:讀R功能輸出列
rpy2 fails to import 'rgl' R package
我的目標是要檢查如果某些包內裝R
從內python
。
繼Dirk Eddelbuettel,我使用的installed.packages()
功能從R
列出所有可用的軟件包上his answer在評論中給出的建議。
這是我到目前爲止有:
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
它返回一個相當大的輸出的形式爲:
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"
...
我需要提取安裝包只是名稱,所以無論是第一列還是第二列對我來說都足夠了。
我使用np.loadtxt()
,np.genfromtxt()
和with open(rpack) as csvfile:
試過,但沒有能夠給回一個列表/數組,其中任一列或行被正確分離(他們都失敗,不同的錯誤實際上)。
我怎樣才能以列的形式讀取這個輸出,或者更多的問題,在列表/數組中提取已安裝軟件包的名稱?
解釋downvote的評論會非常好。 – Gabriel 2015-02-24 14:08:01
這不是我,但它可能與說你從這些嘗試中得到的錯誤有關,並沒有發佈錯誤。這裏的基本問題是,你試圖'打開'這個'rpack'對象,就好像它是一個文件(或者實際上,打開一個'rpack'命名的文件)。這些錯誤應該已經很明顯的出了什麼問題。 – will 2015-02-24 14:11:31
謝謝你,我試圖不讓這個問題如此之大。下次我會發布_everything_。乾杯。 – Gabriel 2015-02-24 14:14:16