[R] Problem with generic methods

Martin Maechler maechler at stat.math.ethz.ch
Mon Jun 8 13:23:47 CEST 2009


>>>>> "RMK" == Rainer M Krug <r.m.krug at gmail.com>
>>>>>     on Mon, 8 Jun 2009 11:18:17 +0200 writes:

    RMK> 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

    RMK> I am trying to use S4, and don't know much about S3 - The is simply a
    RMK> 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)

    RMK> That's it exactly.

well, 
as Martin Morgan told you :

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

it's quite *un*usual to explicitly ``define a function'' for the
method, i.e. to assign it to a variable before using it.
But I agree that this is always a matter of taste, as well.

    RMK> That also would have been an option.

yes,  "the usual S4-like"  option.

Martin Maechler, ETH Zurich

    RMK> Thanks,
    RMK> 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
    >> 



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

    RMK> ______________________________________________
    RMK> R-help at r-project.org mailing list
    RMK> https://stat.ethz.ch/mailman/listinfo/r-help
    RMK> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    RMK> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list