Last active
November 5, 2015 09:48
-
-
Save guidocor/ba3ba9c76c1b5dffe99c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--------------------------------------------------------------- | |
# INSTALL MY PACKAGES SNIPPET install.R | |
#--------------------------------------------------------------- | |
# Script to detach all previous packages loaded, install those who you | |
# use and import them. Change the needed argument to install others or paste to your script | |
# the install_my_packages() fucntion and give other argument | |
# | |
# USEFUL: | |
# use this code to your source script to | |
# rm(list=ls()) | |
# setwd("~/home/project/") | |
# source("./utils/install.R") | |
# install_and_detach() | |
install_and_detach <- function(needed = c( | |
"Rmisc", | |
"dplyr", | |
"doBy", | |
"lme4", | |
"gmodels", | |
"lattice", | |
"ggplot2"), | |
clean = TRUE, | |
load = FALSE ) | |
{ | |
# @needed is a vector of package to install. Set the default option as your basics | |
# @clean logical. If TRUE, all all packages are detached | |
# @load logical. If TRUE, all packages will be loaded | |
if(clean == TRUE ) { | |
# taken from: http://stackoverflow.com/questions/7505547/detach-all-packages-while-working-in-r | |
basic.packages <- c("package:stats","package:graphics","package:grDevices", | |
"package:utils","package:datasets","package:methods","package:base") | |
package.list <- search()[ifelse(unlist(gregexpr("package:",search()))==1,TRUE,FALSE)] | |
package.list <- setdiff(package.list,basic.packages) | |
if (length(package.list)>0) for (package in package.list) detach(package, character.only=TRUE) | |
} | |
unlist(lapply(needed, FUN = function(x){ | |
if(!x %in% installed.packages()){ | |
cat("====================================================\n") | |
cat("The following package will be installed ",x, "\n") | |
install.packages(x) | |
cat("====================================================\n") | |
} | |
if(load == TRUE) | |
{ | |
library(x, character.only = T, verbose = F) | |
return(paste0( x , " successfully loaded")) | |
} | |
})) | |
} |
Thanks : ) I will apply your recomendation.
Updated now has more options : )
Your solution for the loading is very clever and readeable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With lapply may be quiet quicker. So, with "for" it is good enough may be the same speed for both unless you install 1000 packages
needed <- list("ggplot2", "data.table", "png", "jpeg", "grid")
unlist(lapply(needed, FUN = function(x){
if(!x %in% installed.packages()){
cat("====================================================\n")
cat("The following package will be installed ",x, "\n")
install.packages(x)
cat("====================================================\n")
}
library(x, character.only = T, verbose = F)
return(paste0( x , " successfully loaded"))
}))