Skip to content

Instantly share code, notes, and snippets.

@guidocor
Last active November 5, 2015 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidocor/ba3ba9c76c1b5dffe99c to your computer and use it in GitHub Desktop.
Save guidocor/ba3ba9c76c1b5dffe99c to your computer and use it in GitHub Desktop.
#---------------------------------------------------------------
# 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"))
}
}))
}
@hermesh2
Copy link

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"))
}))

@guidocor
Copy link
Author

guidocor commented Nov 4, 2015

Thanks : ) I will apply your recomendation.

@guidocor
Copy link
Author

guidocor commented Nov 5, 2015

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