[R] The object argument of NextMethod.

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Apr 14 19:55:24 CEST 2006


On Fri, 14 Apr 2006, ronggui wrote:

> My question is when the object argument of NexthMethod be used?

Looking at the C code, I don't think it is.  But look at do_nextmethod in 
src/main/objects.c yourself.

Note that in your examples it would not make any difference as 'x' 
is re-evaluated in the current frame, and so passing it explicitly will 
find the same value.

As far as I can tell, in NextMethod(generic, object, ...), 'generic' is 
only used if not invoked via a generic, and only named arguments in ...
are used, to replace or append to the argument list of the current method.

This is an area where R-lang is only a draft, and the definitive 
documentation is the C code.  The lack of precision seems not to be doing 
anyone much harm, as it is normally inadvisable to use NextMethod if you 
want to do anything convoluted (especially if you want it to work in both 
S and R).

> In the following example, weather object argument is used will not
> affects the result.
>
> ###
> foo=function(x)  {UseMethod("foo")}
>
> foo.cls1=function(x)
> {
>  x=x+1;class(x)<-"ncls"
>  NextMethod()
> }
>
> foo.ncls=function(x)
> {
> cat("ncls\n")
> }
>
> foo.cls2=function(x)
> {
> cat("cls2\n");print(x)
> }
>
> a=1;class(a)=c("cls1","cls2")
>
>> foo(a)
> cls2
> [1] 2
> attr(,"class")
> [1] "ncls"
>
> ###
>> foo=function(x)  {UseMethod("foo")}
>>
>> foo.cls1=function(x)
> + {
> +   x=x+1;class(x)<-"ncls"
> +   NextMethod(,x)
> + }
>>
>> foo.ncls=function(x)
> + {
> + cat("ncls\n")
> + }
>>
>> foo.cls2=function(x)
> + {
> + cat("cls2\n");print(x)
> + }
>>
>> a=1;class(a)=c("cls1","cls2")
>>
>> foo(a)
> cls2
> [1] 2
> attr(,"class")
> [1] "ncls"
>
> Thank you very much.
>
> --
> »ÆÈÙ¹ó
> Deparment of Sociology
> Fudan University
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list