[R-pkg-devel] Extending an S3 method, but putting the package in Suggests?

Martin Maechler maechler at stat.math.ethz.ch
Tue Mar 14 10:06:17 CET 2017


>>>>> David Hugh-Jones <davidhughjones at gmail.com>
>>>>>     on Tue, 14 Mar 2017 02:46:35 +0000 writes:
>>>>> David Hugh-Jones <davidhughjones at gmail.com>
>>>>>     on Tue, 14 Mar 2017 02:46:35 +0000 writes:

    > Hi,
    > Cross-posted from SO:
    > http://stackoverflow.com/questions/42776058/extending-an-s3-generic-from-an-optional-package

  ((sent my answer there as well))

    > I have a package which provides an as.FlexTable method for its objects,
    > extending the S3 generic from the ReporteRs package. So, my NAMESPACE file,
    > generated by roxygen, has lines:

    > importFrom(ReporteRs,as.FlexTable)
    > ...
    > S3method(as.FlexTable,huxtable)
    > ...
    > export(as.FlexTable)

    > I don't much want to put ReporteRs in Imports: in the DESCRIPTION file,
    > because it involves a big external dependency on Java. But, when I put it
    > into Suggests:, R CMD check gives me errors like "Namespace dependency not
    > required".

    > Is there anyway I can extend the generic without making a hard dependency?

No.  Importing is a hard dependency..
Some people do not import formally but use  '<pkgNamespace>::<obj>'
instead, *and* conditionalize their code on the availability of
that namespace.
I don't recommend that at all, and particularly not for
extending a generic.

I recommend you talk with the maintainer of 'ReporteRs':
1) You could use a common (yet-to-create) very small package say 'flexS3generics'
   which provides S3 generics (and S4 if ..) you want to use
   both, and then both you and her/him import from that mini package.
   You'd be both authors of that package.

2) If your package is much smaller (in its footprint, incl
   dependencies) than 'ReporteRs' she/he may agree to import the
   S3 generic from your package instead of the other way around.

Both are clean solutions,
and both need some time-coordination when releasing to CRAN,
'1)' being easier: Once the 'flexS3generics' is released to
CRAN, change (both) your package(s) to
importFrom(flexS3generics,*) but these changes and CRAN
submissions are then independent of each other.


    > Cheers,
    > David



More information about the R-package-devel mailing list