[Rd] S4 helper functions: regular or generic?

Martin Morgan mtmorgan at fhcrc.org
Wed Feb 25 15:36:08 CET 2009


Hi Gopi --

Gopi Goswami <grgoswami at gmail.com> writes:

> Hi there,
>
>
> I want to write helper functions for a base class, which will be used
> by its subclasses in the S4 world. This function ___will___ update
> certain slots of its argument object. Please help me decide which one
> of the following is a better approach with respect to coding style,
> memory usage and speed:
>

My opinion:

> o   Write a regular function.

memory and speed

> o   Declare a generic and implement it just for the base class.

coding 'style', but style is subjective.

There are other aspects of S4, e.g., type checking, method dispatch,
programmatically defined and discoverable API, ... (positives),
cumbersome documentation (negative).

My usual pattern of development is to be seduced by the siren of
speed, only to regret boxing myself in.

I find that my S4 objects typically serve as containers for
coordinating other entities.  The important methods typically extract
R 'base' objects from the S4 class, manipulate them, and repackage the
result as S4. The time and speed issues are in the manipulation, not
in the extraction / repackaging. This is contrast to, say, an
implementation of a tree-like data structure with a collection of
'Node' objects, where tree operations would require access to each
object and would be horribly slow in S4 (and perhaps R when nodes were
represented as a list, say, at least compared to a C-level
representation, or an alternative representation that took advantage
of R's language characteristics).

Martin

>
> Thanks for sharing your insight and time,
> gopi.
> http://gopi-goswami.net/
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-devel mailing list