[R-pkg-devel] package with datasets requiring sp

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Jan 17 19:17:19 CET 2022


On 17/01/2022 10:06 a.m., Aurélie Siberchicot wrote:
> Hi,
> 
> I maintain the 'ade4' package which contains data with S4 objects from
> the package 'sp'.
> Some functions of the 'sp' package are also used in examples in some Rd
> files (using the code -- if(requireNamespace("sp", quietly = TRUE)) {} --).
> In February 2020, I moved the 'sp' package from 'Suggests' to 'Imports'
> according to an email received from Brian Ripley, because "At least one
> of the datasets cannot be loaded without a package declared in Suggests:
> -- typically this is because it loads a namespace defining S4 classes,
> and such imports should be declared in Imports or (as Matrix does) use a
> .R wrapper."
> 
> For a few months, I have had the following NOTE:
> * checking dependencies in R code ... NOTE
> Namespace in Imports field not imported from: ‘sp’
> All declared Imports should be used.
> 
> In 'Writing R Extensions', I found: "Packages needed to use datasets
> from the package should be in ‘Imports’: this includes those needed to
> define S4 classes used."
> 
> Moving back 'sp' in 'Suggests' seems to solves the NOTE but is contrary
> to the recommendations of 'Writing R Extensions' and Brian Ripley.
> I'm a bit confused to solve this note and the best way to declare the
> dependency of 'ade4' to 'sp'.
> Is anyone in my situation? Does anyone have any advice?

I think the easiest is to follow Prof Ripley's second suggestion:  "use 
a .R wrapper".  To do this, you don't put the .rda file in the data 
directory, you put it somewhere else.  Matrix uses inst/external.  In 
your data directory, you put a small script.

For example, if the object name is foo, you might have the saved object 
in inst/external/foo.rda, and in the data directory a script foo.R that 
looks something like this:

   if (!requireNamespace("sp")) stop("The sp package is required for foo.")

   foo <- readRDS(system.file("external/foo.rda", package = "ade4"))

Duncan Murdoch



More information about the R-package-devel mailing list