需要最低版本的R包

时间:2022-02-27 18:50:54

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?

我只是注意到R的require()或library()函数没有版本参数。当人们需要确保他们至少拥有一些包的最低版本时,他们会怎么做,以便例如他们知道一些错误是修复的,或者某些功能是否可用,或者其他什么?

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.

我知道包装作者的Depends东西,但我正在寻找在脚本,交互式环境,组织模式文件,代码片段等中使用的东西。

3 个解决方案

#1


7  

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):

我不知道这样的功能,但制作一个应该很容易。您可以将其基于sessionInfo()或packageVersion()。加载脚本所需的包后,您可以从那里收集包编号。检查版本号的函数看起来像(伪代码,因为我现在没有时间):

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.

您仍然需要将版本号解析为允许比较cur_version 的内容,但基本结构保持不变。

#2


14  

You could use packageVersion():

你可以使用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).

这是有效的,因为packageVersion()返回类package_version的对象,其行为与我们所希望的一样(当使用字典顺序比较两个字符串时, <不会这样做)。< p>

#3


11  

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

在阅读了Paul的伪代码后,这是我写的函数。

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:

它的功能与library()基本相同,但需要额外的版本参数:

> 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

#1


7  

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):

我不知道这样的功能,但制作一个应该很容易。您可以将其基于sessionInfo()或packageVersion()。加载脚本所需的包后,您可以从那里收集包编号。检查版本号的函数看起来像(伪代码,因为我现在没有时间):

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.

您仍然需要将版本号解析为允许比较cur_version 的内容,但基本结构保持不变。

#2


14  

You could use packageVersion():

你可以使用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).

这是有效的,因为packageVersion()返回类package_version的对象,其行为与我们所希望的一样(当使用字典顺序比较两个字符串时, <不会这样做)。< p>

#3


11  

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

在阅读了Paul的伪代码后,这是我写的函数。

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:

它的功能与library()基本相同,但需要额外的版本参数:

> 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