Skip to content

Instantly share code, notes, and snippets.

@brentthorne brentthorne/ipak_dev.R
Last active Jun 14, 2019

Embed
What would you like to do?
Install and load multiple R packages at once using devtools:install_x() options
# ipak_dev function: install and load multiple R packages using 'devtools::install_' options.
# check to see if packages are installed. Install them if they are not, then load them into the R session.
#
# install_method = "github" is the default which creates "devtools::install_github('examplerepo/examplepackage')".
# "github" can be replaced with any of the default install options for devtools, such at c("git","bitbucket","cran","local" etc..
# NOTE not all of the options have been tested as I created this for my own personal use which is mainly through github.
#
# install_method = "base" is if you have a list of packages that are to be installed using the base install.packaes() function
#
# This script was adapted from the ipak function created by @stevenworthington
ipak_dev <- function(pkg, base.pkg = NULL, install_method = "github"){
if(is.null(base.pkg) == FALSE){
new.base.pkg <- base.pkg[!(base.pkg %in% installed.packages()[, "Package"])]
if (length(new.base.pkg))
install.packages(new.base.pkg, dependencies = TRUE)
#split string to search for previouslly already installed packages
spl.pkg <- strsplit(pkg, split = "/")
dev.pkg <- unlist(lapply(1:length(pkg), function(x) spl.pkg[[x]][2]))
rep.pkg <- unlist(lapply(1:length(pkg), function(y) spl.pkg[[y]][1]))
df.pkg <- data.frame(rep.pkg,dev.pkg, pkg)
lst.pkg <- dev.pkg[!(dev.pkg %in% installed.packages()[, "Package"])]
#list of packages to install
new.pkg <- df.pkg[df.pkg$dev.pkg == lst.pkg,3]
#create the devtools string for each package
install_method_tmp <- paste("devtools::install",install_method, sep="_")
install_method_test <- paste(install_method_tmp,"('", new.pkg,"')", sep="")
if (length(new.pkg) > 0){
#install and require
eval(parse(text=install_method_test))
}else{}
#combine both lists
all.pkg <- c(dev.pkg,base.pkg)
sapply(all.pkg, require, character.only = TRUE)
}else{
#split string to search for previouslly already installed packages
spl.pkg <- strsplit(pkg, split = "/")
dev.pkg <- unlist(lapply(1:length(pkg), function(x) spl.pkg[[x]][2]))
rep.pkg <- unlist(lapply(1:length(pkg), function(y) spl.pkg[[y]][1]))
df.pkg <- data.frame(rep.pkg,dev.pkg, pkg)
lst.pkg <- dev.pkg[!(dev.pkg %in% installed.packages()[, "Package"])]
#list of packages to install
new.pkg <- df.pkg[df.pkg$dev.pkg == lst.pkg,3]
#create the devtools string for each package
install_method_tmp <- paste("devtools::install",install_method, sep="_")
install_method_test <- paste(install_method_tmp,"('", new.pkg,"')", sep="")
if (length(new.pkg)){
#install and require
eval(parse(text=install_method_test))}
sapply(dev.pkg, require, character.only = TRUE)
}
}
# usage
dev.packages <- c("tidyverse/ggplot2",
"rstudio/rticles",
"rstudio/rmarkdown",
"hadley/tidyverse",
"yihui/tinytex",
"haozhu233/kableExtra",
"crsh/citr",
"wch/extrafont",
"jrnold/ggthemes"
)
packages <- c("rgdal","Cairo", "mapproj","ggspatial")
ipak_dev(dev.packages, base.pkg = packages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.