我希望达到某种效果
libs = c("AER", "gbm", "caret", "MASS", "nnet", "randomForest")
for (i in libs) {
if(!is.element(i, .packages()) {
install.packages(i)
}
library(i)
}
对库(i)的调用不会将i替换为存储在变量i中的值。相反,它尝试加载名为" i"
的库有没有办法强制i在调用库之前被视为变量并解决?
答案 0 :(得分:25)
library(...,character.only = TRUE)
怎么样?
答案 1 :(得分:3)
这是完整的代码(结合了joran的回答并添加" all.available = TRUE")。
libs = c("AER", "gbm", "caret", "MASS", "nnet", "randomForest")
for (i in libs){
if( !is.element(i, .packages(all.available = TRUE)) ) {
install.packages(i)
}
library(i,character.only = TRUE)
}