Skip to content

Instantly share code, notes, and snippets.

@hakimabdi
Last active January 30, 2021 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hakimabdi/eadddfd66b05ffd8a9cb to your computer and use it in GitHub Desktop.
Save hakimabdi/eadddfd66b05ffd8a9cb to your computer and use it in GitHub Desktop.
This function is for the installation and loading of R packages.. If an R package is not installed, the function will install it using the Austrian CRAN mirror (you can change that), and if a package is already installed, it will be loaded.
#################################################################################################
# title : pload.R
# purpose : Install and load uninstalled packages, or load installed ones.
# author : Abdulhakim Abdi (@HakimAbdi)
# input : Package name in quotes
# output : Installation and loading of uninstalled packages, or loading of installed ones.
#################################################################################################
pload <- function(x){
if(x %in% rownames(installed.packages()))
#if (require(x,character.only = TRUE))
require(x,character.only = TRUE)
else {
install.packages(x,dep=TRUE, repos="http://cran.at.r-project.org/")
require(x,character.only = TRUE)
}
}
## Example:
# pload("raster")
# pload("maptools")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment