[R-pkg-devel] Two packages with the same generic function

Neal Fultz n|u|tz @end|ng |rom gm@||@com
Mon Jun 22 20:35:50 CEST 2020


Another decent alternative is to submit the generic function to the
`generics` package - https://github.com/r-lib/generics - or create
your own API package.

Both package A and B can import the generic from it, and everything
works fine. It may also reduce your dependencies and/or build time.

This is much simpler than the workarounds we had tried to optionally
allow use of tidy() from DeclareDesign without transitively importing
the entire tidyverse, for example.

We had tried various ways of delegating from pkgA::foo.default to
pkgB::foo but it had some edge cases around the order that the user
attached the packages IIRC.

On Mon, Jun 22, 2020 at 11:01 AM Duncan Murdoch
<murdoch.duncan using gmail.com> wrote:
>
> On 22/06/2020 1:40 p.m., Bert Gunter wrote:
> > ...
> > and just to add to the query, assume the author of pkg B did (does) not
> > know of pkg A and so, for example, could (did) not import any of pkg A's
> > content into B. Given that there are at the moment ~20,000 packages out
> > there, this does not seem to be an unreasonable assumption. One may even
> > further assume that the user may not know that (s)he has package B loaded,
> > as it may be a dependency of another package that (s)he uses. I certainly
> > don't keep track of all the dependencies of packages I use.
> >
> > Under these assumptions, is there any more convenient alternative to
> > Wolfgang's pkgA:foo(x) explicit call under such assumptions? If pkgA has a
> > long name, what might one do?
>
> It's always possible to make a new name, e.g.
>
> fooA <- pkgA::foo
> fooB <- pkgB::foo
>
> If you are writing a package, this can be done in the NAMESPACE file, e.g.
>
> importFrom(pkgA, fooA = foo)
>
> though this doesn't appear to be documented in the usual places.
>
> Duncan Murdoch
>
>
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Mon, Jun 22, 2020 at 10:00 AM Viechtbauer, Wolfgang (SP) <
> > wolfgang.viechtbauer using maastrichtuniversity.nl> wrote:
> >
> >> Hi All,
> >>
> >> Let's say there are two packages pkgA and pkgB, both of which have a
> >> generic function
> >>
> >> foo <- function(x, ...)
> >>     UseMethod("foo")
> >>
> >> and pkgA has a method for objects of class "A":
> >>
> >> foo.A <- function(x, ...)
> >>     print(x)
> >>
> >> and pkgB has a method for objects of class "B":
> >>
> >> foo.B <- function(x, ...)
> >>     plot(x)
> >>
> >> Both packages export foo and their method and declare their respective S3
> >> methods, so:
> >>
> >> export(foo)
> >> export(foo.A)
> >> S3method(foo, A)
> >>
> >> in NAMESPACE of pkgA and
> >>
> >> export(foo)
> >> export(foo.B)
> >> S3method(foo, B)
> >>
> >> in NAMESPACE of pkgB.
> >>
> >> If a user loads pkgA first and then pkgB, this fails:
> >>
> >> library(pkgA)
> >> library(pkgB)
> >> x <- 1:4
> >> class(x) <- "A"
> >> foo(x)
> >>
> >> Error in UseMethod("foo") :
> >>    no applicable method for 'foo' applied to an object of class "A"
> >>
> >> and vice-versa. Of course, pkgA::foo(x) works. Aside from pkgA importing
> >> foo() or vice-versa, is there some other clever way to make this work? In
> >> earlier versions of R (at least in 3.6.3), this used to work (i.e., the
> >> generic foo() from pkgB would find method foo.A() and vice-versa), but not
> >> since 4.0.0.
> >>
> >> Best,
> >> Wolfgang
> >>
> >> ______________________________________________
> >> R-package-devel using r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-package-devel
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-package-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-package-devel
> >
>
> ______________________________________________
> 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