[R-pkg-devel] using a class from a suggested package

Thierry Onkelinx th|erry@onke||nx @end|ng |rom |nbo@be
Wed Feb 9 12:25:14 CET 2022


Dear Ivan,

Thanks for the suggestion. Something strange is happening. When I run
getClassDef("inla", package = "INLA") is a fresh session I get.

Virtual Class "inla" [package "INLA"]

Slots:

Name:   .S3Class
Class: character

Extends: "oldClass"

However when package A is loaded first, I get. Since package B imports
package A, the namespace of A is loaded when building B.

Virtual Class "inla" [package "A"]

Slots:

Name:   .S3Class
Class: character

Extends: "oldClass"
Building package B with the code below yields Warning: class "inla" is
defined (with package slot ‘A’) but no metadata object found to revise
superClass information---not imported?  Making a copy in package ‘B’

setClassUnion("maybeInla", "NULL")
#' @importFrom methods getClassDef setIs
.onLoad <- function(...) {
  if (requireNamespace("INLA", quietly = TRUE)) {
    setIs("inla", "maybeInla", classDef = getClassDef("inla", package =
"INLA"))
  }
}
Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx using inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>


Op wo 9 feb. 2022 om 10:45 schreef Ivan Krylov <krylov.r00t using gmail.com>:

> On Mon, 7 Feb 2022 15:55:18 +0100
> Thierry Onkelinx <thierry.onkelinx using inbo.be> wrote:
>
> > My package A defines an S4 method for the "inla" S3 class. The INLA
> > package defines this class. That package has a huge source, making
> > the build fail on r-universe.dev. The solution is to not
> > importClassesFrom(INLA, inla) but use setOldClass("inla"). Then I can
> > list INLA under Suggests instead of Imports. So far so good.
>
> This is a really complicated situation to be in.
>
> On the one hand, the only documented way to use S4 classes from another
> package is NAMESPACE and importClassesFrom(), which leads to stronger
> dependencies than you would like. On the other hand, you're likely to
> get warnings if more than one loaded namespace define S4 classes with
> the same name, to it's a good idea to avoid calling setOldClass('inla')
> in the A package if INLA can be loaded.
>
> Could it be an option to define the "maybeInla" class in the A package,
> initially as "NULL", and then extend it at runtime using getClassDef?
>
> setClassUnion('maybeInla', 'NULL')
>
> .onLoad <- function(...) {
> # ...
>  if (requireNamespace('INLA', quietly = TRUE)) {
>   setIs(getClassDef('inla', package = 'INLA'), 'maybeInla')
>  }
> # ...
> }
>
> If INLA can't be loaded, then there's (ostensibly) no way to create
> "inla" objects in the current session, and "NULL" is as good a
> definition as it would get. Then B could import the maybeInla class
> from A.
>
> --
> Best regards,
> Ivan
>

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list