Index: src/library/base/man/library.Rd =================================================================== --- src/library/base/man/library.Rd (revision 58980) +++ src/library/base/man/library.Rd (working copy) @@ -21,7 +21,7 @@ character.only = FALSE, logical.return = FALSE, warn.conflicts = TRUE, quietly = FALSE, keep.source = getOption("keep.source.pkgs"), - verbose = getOption("verbose")) + verbose = getOption("verbose"), version) require(package, lib.loc = NULL, quietly = FALSE, warn.conflicts = TRUE, @@ -59,6 +59,9 @@ \item{quietly}{a logical. If \code{TRUE}, no message confirming package loading is printed, and most often, no errors/warnings are printed if package loading fails.} + \item{version}{the minimum acceptable version of the package to load. + If a lesser version is found, the package will not be loaded and an + exception will be thrown.} } \details{ \code{library(package)} and \code{require(package)} both load the @@ -189,6 +192,10 @@ search() # "splines", too detach("package:splines") +# To require a specific minimum version: +library(splines, '2.14') +detach("package:splines") + # if the package name is in a character vector, use pkg <- "splines" library(pkg, character.only = TRUE) Index: src/library/base/R/library.R =================================================================== --- src/library/base/R/library.R (revision 58980) +++ src/library/base/R/library.R (working copy) @@ -32,7 +32,7 @@ function(package, help, pos = 2, lib.loc = NULL, character.only = FALSE, logical.return = FALSE, warn.conflicts = TRUE, quietly = FALSE, keep.source = getOption("keep.source.pkgs"), - verbose = getOption("verbose")) + verbose = getOption("verbose"), version) { if (!missing(keep.source)) warning("'keep.source' is deprecated and will be ignored") @@ -276,6 +276,11 @@ stop(gettextf("%s is not a valid installed package", sQuote(package)), domain = NA) pkgInfo <- readRDS(pfile) + if (!missing(version)) { + pver <- pkgInfo$DESCRIPTION["Version"] + if (compareVersion(pver, as.character(version)) < 0) + stop("Version ", version, " of '", package, "' required, but only ", pver, " is available") + } testRversion(pkgInfo, package, pkgpath) ## avoid any bootstrapping issues by these exemptions if(!package %in% c("datasets", "grDevices", "graphics", "methods", @@ -332,10 +337,18 @@ stop(gettextf("package %s does not have a NAMESPACE and should be re-installed", sQuote(package)), domain = NA) } - if (verbose && !newpackage) - warning(gettextf("package %s already present in search()", - sQuote(package)), domain = NA) + if (!newpackage) { + if (verbose) + warning(gettextf("package %s already present in search()", + sQuote(package)), domain = NA) + if (!missing(version)) { + pver <- packageVersion(package) + if (compareVersion(as.character(pver), as.character(version)) < 0) + stop("Version ", version, " of '", package,"' required, ", + "but a lesser version ", pver, " is already loaded") + } + } } else if(!missing(help)) { if(!character.only)