我在rprofile.site中使用此代码在启动r时加载包。
pkg <- c("dplyr", "tidyr", "crayon", "xlsx")
apply(as.matrix(pkg), 1, function(x) {
if (!x %in% utils::installed.packages()) {
utils::install.packages(x)
cat(paste0("package ", x, " installed\n"))
}
x <- require(x, character.only = T)
})
它很好用,不过是印刷品
[1] TRUE TRUE TRUE TRUE
到控制台。我知道我可以转移注意力
stdout
使用
textConnection(); sink(); [code]; sink(); close()
但这似乎是很多工作。有没有办法减少打字量?
tc <- textConnection("outputs","w")
sink(tc, type="output")
pkg <- c("dplyr", "tidyr", "crayon", "xlsx")
apply(as.matrix(pkg), 1, function(x) {
if (!x %in% utils::installed.packages()) {
utils::install.packages(x)
cat(paste0("package ", x, " installed\n"))
}
require(x, character.only = T)
})
sink(NULL, type="output")
close(tc)