Search code examples
rr-package

Enforce upper bound for R Package version dependency


When developing R packages, we use the DESCRIPTION file to list package dependencies (e.g. in the "Imports" section) and we're allowed to specify a minimum version required for each package dependency by suffixing with "(>= x.x.x)". However, what is the analogous feature to specify the maximum version?

Some context

Sometimes the external packages that we depend are released with breaking changes that may no longer be compatible with the package being developed. Updating our package each time external packages are updated with breaking changes isn't sustainable.

Example

Let's say I'm developing an R package called "MarksPackage" and it depends on "dplyr". Everything that I've built so far works just fine with the current state of dplyr v1. However, tomorrow dplyr v2 is released with breaking changes that affect my package. For new users that are insalling my package may have installed the latest dplyr v2 and so MarksPackage won't work for them. I don't necessarily want to or am able to go update my package each time dplyr is updated with breaking changes. I'd rather specify a maximum version, or even a version range, for which I know MarksPackage will work.

How do I accomplish this within the DESCRIPTION file? Is there a syntax like:

Package: MarksPackage
Imports: 
    dplyr (>= 0.8.5 & <=1.2.0),
    dbplyr,
    tidyr (<= 1.0.2)

Solution

  • Whenever you have a question about how R extension packages work, you should consult the Writing R Extensions manual. Here in particular, you want Section 1.1.3, Package Dependencies, which explains in relevant part

    The ‘Depends’ field gives a comma-separated list of package names which this package depends on. Those packages will be attached before the current package when library or require is called. Each package name may be optionally followed by a comment in parentheses specifying a version requirement. The comment should contain a comparison operator, whitespace and a valid version number, e.g. ‘MASS (>= 3.1-20)’.

    ...

    A package or ‘R’ can appear more than once in the ‘Depends’ field, for example to give upper and lower bounds on acceptable versions.

    So, for your example, you'd do

    Package: MarksPackage
    Imports: 
        dplyr (>= 0.8.5),
        dplyr (<= 1.2.0),
        dbplyr,
        tidyr (<= 1.0.2)