Created
October 14, 2014 19:59
-
-
Save jarad/bdecaa3d22441d6e1aa4 to your computer and use it in GitHub Desktop.
Function to install (if necessary) and load R 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
# 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 ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This functionality is now, and probably was before, in pacman::p_load.