[R] Problem with generic methods

Rainer M Krug r.m.krug at gmail.com
Mon Jun 8 11:18:17 CEST 2009


On Sat, Jun 6, 2009 at 12:14 AM, Martin Morgan<mtmorgan at fhcrc.org> wrote:
> Rainer M Krug <r.m.krug at gmail.com> writes:
>
>> Hi
>>
>> I want to create a new generic method, but I end up with an error
>> (evaluation nested too deeply). see the transcript below.
>> The function beginYear.Fun() works, but not beginYear.
>>
>> I have no idea why.
>>
>> Any ideas welcome,
>>
>> Rainer
>>
>>
>>> setClass("fun", representation(x = "numeric"))
>> [1] "fun"
>>> new("fun")
>> An object of class “fun”
>> Slot "x":
>> numeric(0)
>>
>>> setGeneric(
>>            "beginYear",
>>            function(object, ...) standardGeneric("beginYear")
>> )
>> setGeneric(
>> +            "beginYear",
>> +            function(object, ...) standardGeneric("beginYear")
>> + )
>> [1] "beginYear"
>>
>>> beginYear.Fun = function(x) x^2
>>> beginYear.Fun(4)
>> [1] 16
>
> Are you confusing S3 and S4 method systems? The .Fun has no special
> meaning in S4

I am trying to use S4, and don't know much about S3 - The is simply a
separator - I probably should have used _ instead.

>
>>> setMethod(
>>           "beginYear",
>>           signature( object = "numeric" ),
>>           beginYear
>>           )
>
> here the beginYear,numeric-method is defined to invoke the beginYear
> generic, so you end up calling yourself. You might have meant
>
>  setMethod(beginYear, signature(object="numeric"), beginYear.Fun)

That's it exactly.

>
> but I would have written
>
>  setMethod(beginYear, signature(object="numeric"),
>            function(object, ...) object^2)

That also would have been an option.

Thanks,

Rainer

>
> Martin
>
>> setMethod(
>> +           "beginYear",
>> +           signature( object = "numeric" ),
>> +           beginYear
>> +           )
>> [1] "beginYear"
>>
>>> beginYear.Fun(4)
>> [1] 16
>>
>>> beginYear(4)
>> Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
>>>
>
> --
> Martin Morgan
> Computational Biology / Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N.
> PO Box 19024 Seattle, WA 98109
>
> Location: Arnold Building M1 B861
> Phone: (206) 667-2793
>



-- 
Rainer M. Krug, Centre of Excellence for Invasion Biology,
Stellenbosch University, South Africa




More information about the R-help mailing list