I use this code in my Rprofile.site to load packages when I start 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)
})
It works just fine but prints [1] TRUE TRUE TRUE TRUE
to the console. I know I can divert stdout
using textConnection(); sink(); [code]; sink(); close()
, but that seems like a lot of work. Is there a way to do this with less typing?
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)