Search code examples
rpackageversion

r check if package version is greater than x.y.z


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.


Solution

  • 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 and R_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.