Skip to content

Instantly share code, notes, and snippets.

@jarad
Created October 14, 2014 19:59
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 jarad/bdecaa3d22441d6e1aa4 to your computer and use it in GitHub Desktop.
Save jarad/bdecaa3d22441d6e1aa4 to your computer and use it in GitHub Desktop.
Function to install (if necessary) and load R packages
# From http://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
install_and_load_packages <- function(x){
for( i in x ){
# require returns TRUE invisibly if it was able to load package
if( ! require( i , character.only = TRUE ) ){
# If package was not able to be loaded then re-install
install.packages( i , dependencies = TRUE )
# Load package after installing
require( i , character.only = TRUE )
}
}
}
@jarad
Copy link
Author

jarad commented Oct 25, 2016

This functionality is now, and probably was before, in pacman::p_load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment