R packages have version numbers like 1.97.1. I can check what the version number is with
packageVersion("data.table")
On my computer this returns 1.10.0.
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.
1 Answer. You can use the packageVersion() function to print version information about the loaded packages. To print the version information about R, the OS and attached or loaded packages, use the sessionInfo() function.
To install a specific version of a package, we need to install a package called “remotes” and then load it from the library. Afterwards we can use install_version() by specifying the package name and version needed as shown below.
Installing an older package from source If you know the URL to the package version you need to install, you can install it from source via install. packages() directed to that URL. If you don't know the URL, you can look for it in the CRAN Package Archive.
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.
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
if a < b
0
if a == b
1
if a > b
Source:
?utils::compareVersion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With