Skip to content
Prev 6400 / 12125 Next

[R-pkg-devel] Accessing features in R4.0.

Colin, you can do:

Imports: tools

NAMESPACE:
if (getRversion() >= 4) importFrom(tools,R_user_dir)


R/000.namespace.R:
## Create a dummy R_user_dir() for R (< 4.0.0)
## to please R CMD check
if (getRversion() < 4) {
  R_user_dir <- function(...) NULL
}

and then use:

if (getRversion() < 4) {
  # do something
} else {
  R_user_dir("oysteR", which = "cache")
}

An advantage of this approach is that it's clear from Imports: and the
NAMESPACE file what you're importing and when.  When using Suggests:
and pkg::fcn() you can't peek at NAMESPACE to see what's actually
used.


Finally, if '#do something' is basically trying to do the same as
tools::R_user_dir(), you could of course also consider writing your
own forward-compatible wrapper for R (< 4.0.0), e.g.

if (getRversion() < 4) {
  R_user_dir <- function(...) {
    # do something
   }
}

and then use R_user_dir() as if you're running R (>= 4.0.0).  That's
the cleanest version.

Hope this helps,

Henrik


On Wed, Dec 16, 2020 at 11:12 AM Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote: