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

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Wed Dec 16 21:38:59 CET 2020


BTW, 'backports' provides a backport for tools::R_user_dir() also for
R (< 4.0.0), so an alternative solution in this case is:

Imports: backports

and

importFrom(backports, R_user_dir)

The 'backports' package takes the same approach as I did in my previous message.

Henrik

On Wed, Dec 16, 2020 at 12:27 PM Henrik Bengtsson
<henrik.bengtsson using gmail.com> wrote:
>
> 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 using dcn.davis.ca.us> wrote:
> >
> > For "obvious" reasons? I don't see this kind of avoidance as "obviously" correct at all. You have a dependency... it should be declared. There are various ways to proceed, with Imports or Depends or Suggests or pulling the code into your package... but trying to subvert the dependency management is counterproductive.
> >
> > On December 16, 2020 8:28:15 AM PST, Colin Gillespie <csgillespie using gmail.com> wrote:
> > >Hi,
> > >
> > >I'm planning on using the tools::R_user_dir function in a package. But
> > >for obvious reasons, I don't want to set the dependency on R 4.
> > >
> > >My code is basically
> > >
> > >if (as.numeric(R.version$major) < 4) # do something
> > >else tools::R_user_dir("oysteR", which = "cache")
> > >
> > >When checking on win-builder R3.6 I get the note
> > >
> > >* checking dependencies in R code ... NOTE
> > >Missing or unexported object: 'tools::R_user_dir'
> > >
> > >## Question
> > >
> > >Is my code correct and can I ignore this note?
> > >
> > >Thanks
> > >
> > >Colin
> > >
> > >
> > >Dr Colin Gillespie
> > >http://www.mas.ncl.ac.uk/~ncsg3/
> > >
> > >______________________________________________
> > >R-package-devel using r-project.org mailing list
> > >https://stat.ethz.ch/mailman/listinfo/r-package-devel
> >
> > --
> > Sent from my phone. Please excuse my brevity.
> >
> > ______________________________________________
> > R-package-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list