Re: [R] Simple methods question

From: Jan T. Kim <jtk_at_cmp.uea.ac.uk>
Date: Fri 22 Apr 2005 - 02:02:48 EST

On Thu, Apr 21, 2005 at 04:35:07PM +0100, Jan T. Kim wrote:
> On Thu, Apr 21, 2005 at 03:00:07PM +0200, Uwe Ligges wrote:
> > Sean Davis wrote:
> >
> > >I would like to create a function with methods with the same behavior
> > >for both numeric and logical arguments (using S4 methods) and different
> > >for matrix.
> > >
> > >I would typically do:
> > >
> > >setGeneric('foo',function(x) standardGeneric('foo'))
> > >setGeneric('foo','numeric',function(x) {...stuff 1...})

[...]

> > >setGeneric('foo','logical',function(x) {...stuff 1...})
> > >setGeneric('foo','matrix',function(x) {....stuff 2...})
> > >
> > >If "stuff1" is identical for numeric and logical, can the two
> > >setGenerics be "combined" somehow?
> >
> > Maybe using (implicit) inheritance?
>
> If the main point is to avoid duplication of the ...stuff 1... code,
> I'd suggest assigning that function to an identifier and using that in
> the setMethod calls, as in
>
> stuff1 <- function(x)
> {
> ...stuff 1...
> }
>
>
> stuff2 <- function(x)
> {
> ...stuff 2...
> }
>
> setMethod('foo', 'numeric', stuff1);
> setMethod('foo', 'logical', stuff1);
> setMethod('foo', 'matrix', stuff1);

                                 ^^^^^^

Just to avoid any possible confusion: This should have been "stuff2", of course.

Sorry for this fumble.

Best regards, Jan

-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk@cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Apr 22 01:25:40 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:21 EST