我有一个脚本用于确定是否已安装软件包,如果没有安装它:
if("abc" %in% rownames(installed.packages())==FALSE) {install.packages("abc")}
我想将单一检查扩展到包列表,例如ls(“abc”,“MCMCpack”,“BMA”,“coda”),这样我就可以简单地使用一个功能并在闲暇时编辑列表。但我需要一次一个功能检查列表。有什么建议吗?
答案 0 :(得分:1)
只是为了好玩,你可以使用这样的东西......
check.package <- function(package){
if(!package %in% .packages(all = TRUE)){
cat("Package", package, "is not installed. Installing ", package, "...\n")
install.packages(package)
library(package)
}
else{
cat("Package", package, "is already installed.\n")
}
}
实施例
> check.package("strucchange")
Package strucchange is already installed.
> check.package("midasr")
Installing package into ‘/home/aaaa/R/x86_64-pc-linux-gnu-library/3.0’
.....
检查包裹清单:
> invisible(sapply(list("midasr", "strucchange"), check.package))
Package midasr is already installed.
Package strucchange is already installed.
答案 1 :(得分:0)
list
对象,我不清楚所期望的行为......但是你想要这样的东西吗?
check.install.packs <-
function( ps ){
if( any( ti <- !( ps %in% installed.packages() ) ) )
install.packages( ps[ ti ] )
}
然后
check.packs( c( 'survey' , 'MonetDB.R' ) )
然后
check.packs( c( 'survey' , 'SAScii' ) )