[Bioc-devel] BiocGenerics / NAMESPACE question

Hervé Pagès hpages at fhcrc.org
Mon Dec 5 23:20:03 CET 2011


On 11-12-05 01:29 PM, Kasper Daniel Hansen wrote:
> 2011/12/5 Hervé Pagès<hpages at fhcrc.org>:
>
>> Right. I found this in the "Writing R Extensions" manual:
>>
>>   Note that exporting methods on a generic in the namespace will also
>>   export the generic, and exporting a generic in the namespace will
>>   also export its methods.
>>
>> I didn't know this. But it sounds more like a "convenience" feature
>> to me and maybe it's "cleaner design" to not to rely on it?
>
> I find it completely natural that exportMethod will also export the
> generic since there cannot really be a method without a generic.  That
> export also exports all methods is perhaps not completely obvious but
> the only possible usecase would be if you want to hide some methods,
> which I cannot really imagine.
>
> R-devel exts says
>
> "Where a generic has been created in the package solely to add S4
> methods to it, it can be declared via either or both of exports or
> exportMethods, but the latter seems clearer (and is used in the stats4
> example above). On the other hand, where a generic is created in a
> package without setting any methods for it (such as AIC in stats4),
> exports must be used."
>
> and also for imports
>
> "Note that importMethodsFrom will also import any generics defined in
> the namespace on those methods."
>
> Based on this it seems like BiocGenerics is absolutely doing the right
> thing by using export() for generics without methods, but that
> packages in general should use exportMethods and importMethodsFrom in
> order to export/import methods and their generics.

I interpret the above comment from R-devel exts differently: in "but the
latter seems clearer", I think that:

   - the former = the generic is declared via either exports or
                  exportMethods

   - the latter = the generic is declared via both of exports or
                  exportMethods

I also find that the latter is clearer (mostly a matter of taste I
agree). What's confusing is that in the stats4 example they provide,
they don't do the former and they don't do the latter either because
they don't have the situation where a generic and its methods both
need to be exported. All the stuff they have in exportMethods()
corresponds to generics defined in another package! So probably not
the best example to illustrate what they are talking about...

>  Guess that we need
> to use importFrom to catch the generics from BiocGenerics.

Definitely. Alternatively you could just import all of BiocGenerics
with just import(BiocGenerics).

Cheers,
H.

>
> Thanks for the help and for pointing out the discussion in R-exts.
> Kasper


-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the Bioc-devel mailing list