Skip to content

Instantly share code, notes, and snippets.

@wesleyit
Created April 1, 2017 16:58
Show Gist options
  • Save wesleyit/75d6c2c25dabef1e131661b57db2dc5b to your computer and use it in GitHub Desktop.
Save wesleyit/75d6c2c25dabef1e131661b57db2dc5b to your computer and use it in GitHub Desktop.
This snippet will load the libraries in a smarter way. If the library is already installed, then it is loaded. Otherwise, it is installed and then loaded.
ensure_pkg <- function(pkg) {
pkg = toString(substitute(pkg))
if (!require(pkg, character.only = TRUE)){
install.packages(pkg)
require(pkg)
}
}
ensure_pkg("car")
ensure_pkg("lattice")
ensure_pkg("rpart")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment