r check if package version is greater than x.y.z
As suggested by Benjamin, the right tool is compareVersion
:
version_above <- function(pkg, than) {
as.logical(compareVersion(as.character(packageVersion(pkg)), than))
}
packageVersion("ggplot2")
# [1] '2.2.1'
version_above("ggplot2", "2.0.0")
# [1] TRUE
version_above("ggplot2", "3.0.0")
# [1] FALSE
Outcomes of compareVersion(a, b)
are
-1
ifa < b
0
ifa == b
1
ifa > b
Source:
?utils::compareVersion
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
This is illustrated in the examples for ?packageVersion
; the ability to use comparison operators in this way is explicitly documented in ?package_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.