Skip to content

Instantly share code, notes, and snippets.

@raffdoc
Created June 19, 2012 19:35
Show Gist options
  • Save raffdoc/2956081 to your computer and use it in GitHub Desktop.
Save raffdoc/2956081 to your computer and use it in GitHub Desktop.
Wrapper for package installation and loading
# Script name: instant_pkgs.R
# Purpose: Package installation and loading
# Author: Kay Cichini
# Date: 2012-06-19
# Licence: cc by-nc-sa
instant_pkgs <- function(pkgs) {
pkgs_miss <- pkgs[which(!pkgs %in% installed.packages()[, 1])]
if (length(pkgs_miss) > 0) {
install.packages(pkgs_miss)
}
if (length(pkgs_miss) == 0) {
message("\n ...All packages were already installed!\n")
}
# install packages not already loaded:
pkgs_miss <- pkgs[which(!pkgs %in% installed.packages()[, 1])]
if (length(pkgs_miss) > 0) {
install.packages(pkgs_miss)
}
# load packages not already loaded:
attached <- search()
attached_pkgs <- attached[grepl("package", attached)]
need_to_attach <- pkgs[which(!pkgs %in% gsub("package:", "", attached_pkgs))]
if (length(need_to_attach) > 0) {
for (i in 1:length(need_to_attach)) require(need_to_attach[i], character.only = TRUE)
}
if (length(need_to_attach) == 0) {
message("\n ...All packages were already loaded!\n")
}
}
# Examples:
instant_pkgs(c("base", "jpeg"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment