Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require minimum version of R package

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.

like image 905
Ken Williams Avatar asked Feb 16 '12 16:02

Ken Williams


People also ask

What is the require command in R?

The require() is designed to be used inside functions as it gives a warning message and returns a logical value say, FALSE if the requested package is not found and TRUE if the package is loaded. It is better to use the library() as it gives the error message if the package is not found during the package loading time.

Why do we need packages in R?

R packages are a collection of R functions, complied code and sample data. They are stored under a directory called "library" in the R environment. By default, R installs a set of packages during installation. More packages are added later, when they are needed for some specific purpose.


3 Answers

You could use packageVersion():

packageVersion("stats")
# [1] ‘2.14.1’

if(packageVersion("stats") < "2.15.0") {
    stop("Need to wait until package:stats 2.15 is released!")
}
# Error: Need to wait until package:stats 2.15 is released!

This works because packageVersion() returns an object of class package_version for which < behaves as we'd like it to (which < will not do when comparing two character strings using their lexicographical ordering).

like image 154
Josh O'Brien Avatar answered Oct 15 '22 12:10

Josh O'Brien


After reading Paul's pseudocode, here's the function I've written.

use <- function(package, version=0, ...) {
  package <- as.character(substitute(package))
  library(package, ..., character.only=TRUE)
  pver <- packageVersion(package)
  if (compareVersion(as.character(pver), as.character(version)) < 0)
    stop("Version ", version, " of '", package, 
         "' required, but only ", pver, " is available")
  invisible(pver)
}

It functions basically the same as library(), but takes an extra version argument:

> use(plyr, 1.6)
> use(ggplot2, '0.9')
Error in use(ggplot2, "0.9") : 
  Version 0.9 of 'ggplot2' required, but only 0.8.9 is available
like image 26
Ken Williams Avatar answered Oct 15 '22 13:10

Ken Williams


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.

like image 28
Paul Hiemstra Avatar answered Oct 15 '22 13:10

Paul Hiemstra