Skip to content

Instantly share code, notes, and snippets.

@garyfeng
Last active August 29, 2015 13:56
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 garyfeng/a51ac94d857c5f58d501 to your computer and use it in GitHub Desktop.
Save garyfeng/a51ac94d857c5f58d501 to your computer and use it in GitHub Desktop.
r code to test and install libraries if not already installed.
# r code to test and install libraries if not already installed.
# code taking from stackoverflow
pkgInstall <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
library(x, character.only = TRUE)
}
# examples
# pkgInstall("TraMineR")
# pkgInstall("TraMineRextras")
@garyfeng
Copy link
Author

current version loads the library immediately, to save some commands. There are cases where you may not want that. Just comment out this line.

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