Skip to content

Instantly share code, notes, and snippets.

@timvink
Created October 9, 2013 14:05
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 timvink/6901856 to your computer and use it in GitHub Desktop.
Save timvink/6901856 to your computer and use it in GitHub Desktop.
Test whether package is installed. If not, install. Then Load. example use: pkgTest("ggplot2") instead of normal library("ggplot2").
pkgTest <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE, repos="http://cran-mirror.cs.uu.nl/")
if(!require(x,character.only = TRUE)) stop("Package not found, cannot be installed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment