r 检查包版本是否大于 x.y.z [英] r check if package version is greater than x.y.z
问题描述
R 软件包的版本号为 1.97.1.我可以用
R packages have version numbers like 1.97.1. I can check what the version number is with
packageVersion("data.table")
在我的电脑上返回 1.10.0.
On my computer this returns 1.10.0.
我想要做的是检查 data.table 版本是否比 1.9.7 新,因为 1.9.7 之后的版本具有我的代码需要的功能.我尝试将版本拆分为其组成部分并以不同的方式评估它们,但我还没有想出任何可靠的方法来做到这一点.非常感谢任何建议.
What I want to do is check whether the data.table version is newer than say 1.9.7 because versions after 1.9.7 have a feature that my code needs. I've tried splitting the version into its constituent parts and evaluating them in different ways but I haven't figured out any robust way of doing this. Any advice greatly appreciated.
推荐答案
虽然 utils::compareVersion()
很好,但我会说使用 packageVersion()
和比较运算符(如注释中的@G5W 所示)更简单:
While utils::compareVersion()
is fine, I would say that using packageVersion()
with comparison operators (as indicated by @G5W in comments) is simpler:
packageVersion("data.table")
[1] ‘1.10.0’
> packageVersion("data.table")>"1.9.8"
[1] TRUE
> packageVersion("data.table")>"1.10.01"
[1] FALSE
> packageVersion("data.table")=="1.10.0"
[1] TRUE
这在 ?packageVersion
的示例中进行了说明;?package_version
中明确记录了以这种方式使用比较运算符的能力:
This is illustrated in the examples for ?packageVersion
; the ability to use comparison operators in this way is explicitly documented in ?package_version
:
函数 numeric_version
、package_version
和R_system_version
从这样的字符串创建一个表示(如果适合)允许强制和测试,组合,比较、摘要(最小值/最大值)、包含在数据框中,下标和打印.这些类可以持有这样的向量表示.
Functions
numeric_version
,package_version
andR_system_version
create a representation from such strings (if suitable) which allows for coercion and testing, combination, comparison, summaries (min/max), inclusion in data frames, subscripting, and printing. The classes can hold a vector of such representations.
这篇关于r 检查包版本是否大于 x.y.z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!