Re: [R] Simple methods question

From: Sean Davis <sdavis2_at_mail.nih.gov>
Date: Fri 22 Apr 2005 - 01:02:20 EST

On Apr 21, 2005, at 11:35 AM, 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...})
>>
>> Do you mean setMethod()?
>
> Seems to be the case to me -- if not, my subsequent comment may
> be pointless...
>
>>> 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
>
> setMethod('foo', 'numeric', stuff1);
> setMethod('foo', 'logical', stuff1);
> setMethod('foo', 'matrix', stuff1);
>

Thanks Jan and Uwe. That (above) is what I ended up doing and works fine.

Sean



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:16:26 2005

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