Created
November 2, 2015 00:39
-
-
Save tobigithub/16e0171a8c99c0636e10 to your computer and use it in GitHub Desktop.
Install and Load Packages
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
#' Load packages and install packages that are not installed before loading | |
#' | |
#' This function allows you to check install packages then load | |
#' @param packages Vector of packages names to be installed | |
#' @param dependencies Logical. Return package dependencies? | |
#' @param repos Repository for install.packages() | |
#' @keywords install.packages | |
#' @keywords require | |
#' @keywords library | |
#' @export | |
#' @examples | |
#' loadPackages(packages=c('hi','pscl','XML','quantmod','Rcpp','foreign')) | |
loadPackages <- function(packages, dependencies=TRUE, repos="http://cran.rstudio.com/", ...){ | |
pack <- available.packages() #list of all available packages on CRAN | |
#create empty vectors to be returned as a list | |
loaded <- c() | |
installed <- c() | |
error <- c() | |
for(i in 1:length(packages)){ | |
#if package is already installed just load it | |
if (packages[i] %in% row.names(installed.packages()) == TRUE){ | |
#Load the Package | |
library(packages[i], character.only=TRUE, ...) | |
#List it with or without its dependencies | |
if(dependencies==TRUE){ | |
loaded <- rbind(loaded,cbind(packages[i], pack[packages[i],"Depends"])) | |
colnames(loaded)<-c("Package","Dependencies") | |
} else { | |
loaded <- rbind(loaded,packages[i]) | |
colnames(loaded)<-c("Package") | |
} | |
#if package is not already installed install it first checcking for error and then load it | |
} else if (packages[i] %in% row.names(installed.packages()) == FALSE){ | |
if(packages[i] %in% names(pack[,1])==FALSE){ | |
error <- cbind(error, packages[i]) | |
colnames(error)<-c("Package") | |
next | |
} | |
install.packages(packages[i], repos=repos) | |
if(dependencies==TRUE){ | |
installed <- rbind(installed,cbind(packages[i], pack[packages[i],"Depends"])) | |
colnames(installed)<-c("Package","Dependencies") | |
} else { | |
installed <- rbind(installed, packages[i]) | |
colnames(installed)<-c("Package") | |
} | |
# cat("Installed Package",packages[i]) | |
library(c(packages[i]), character.only=TRUE, ...) | |
} | |
} | |
list(loaded=loaded, installed=installed, error=error) | |
} | |
#' Quickly packages and install packages that are not installed before loading | |
#' | |
#' Useful when using knitr. | |
#' This function allows you to check install packages then load | |
#' @param packages Vector of packages names to be installed | |
#' @param repos Repository for install.packages() | |
#' @keywords install.packages | |
#' @keywords require | |
#' @keywords library | |
#' @export | |
#' @examples | |
#' quickPackages(packages=c('hi','pscl','XML','quantmod','Rcpp','foreign')) | |
quickPackages <- function(packages, repos="http://cran.rstudio.com/",...){ | |
#create empty vectors to be returned as a list | |
loaded <- c() | |
installed <- c() | |
for(i in 1:length(packages)){ | |
#if package is already installed just load it | |
if (packages[i] %in% row.names(installed.packages()) == TRUE){ | |
#Load the Package | |
library(packages[i], character.only=TRUE, ...) | |
#List it with or without its dependencies | |
loaded <- rbind(loaded,packages[i]) | |
colnames(loaded)<-c("Package") | |
#if package is not already installed install it first checcking for error and then load it | |
} else if (packages[i] %in% row.names(installed.packages()) == FALSE){ | |
install.packages(packages[i], repos=repos) | |
installed <- rbind(installed, packages[i]) | |
colnames(installed)<-c("Package") | |
library(c(packages[i]), character.only=TRUE, ...) | |
} | |
} | |
list(loaded=loaded, installed=installed) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment