I just noticed that there's no version
argument to R
's require()
or library()
functions. What do people do when they need to ensure they have at least some minimum version of a package, so that e.g. they know some bug is fixed, or some feature is available, or whatever?
I'm aware of the Depends
stuff for package authors, but I'm looking for something to use in scripts, interactive environments, org-mode
files, code snippets, etc.
I am not aware of such a function, but it should be quite easy to make one. You can base it on sessionInfo()
or packageVersion()
. After loading the packages required for the script, you can harvest the package numbers from there. A function that checks the version number would look like (in pseudo code, as I don't have time right now):
check_version = function(pkg_name, min_version) {
cur_version = packageVersion(pkg_name)
if(cur_version < min_version) stop(sprintf("Package %s needs a newer version,
found %s, need at least %s", pkg_name, cur_version, min_version))
}
Calling it would be like:
library(ggplot2)
check_version("ggplot2", "0.8-9")
You still need to parse the version numbers into something that allows the comparison cur_version < min_version
, but the basic structure remains the same.