[R] callNextMethod & initialize & explicit parameters

John Chambers jmc at research.bell-labs.com
Mon May 26 21:55:20 CEST 2003


laurent buffat wrote:
> 
> Hello,
> 
> I know that in general, it is better to call callNextMethod without
> arguments,
> and so the call is done with the current arguments, but I want explicitly
> change the value of
> a given argument, how can I do ?
> 
> See the code. Why is doesn't work ?

You are combining two "nonstandard" mechanisms here that currently don't
quite manage to work together:

1- methods that have different formal argument lists from the generic
function (argument "a", for example).

2- callNextMethod with explicit arguments that use local variables
computed in the current method.

To implement mechanism 1, a local function is created during
setMethod(), and the actual method calls this local function.  The
callNextMethod code tries to take account of these local functions, but
in this case doesn't succeed.  We should be able to fix it.

Meanwhile, a workaround would be to insert the local information
explicitly, replacing callNextMethod(.Object,a=a.init, ...) in the
method for class "B" with two lines such as:
              .Object <- callNextMethod()
              .Object at a <- a.init

(Your example may be a bit exotic for R-help.  intialize() methods and
callNextMethod() are extensions to the "Programming with Data"
description.  Since the details are still being worked out, the R-devel
list might be a better forum.   Also, as a note on portability to
S-Plus:  it currently doesn't have them.)

> 
> Thanks for your help
> 
> Laurent Buffat
> 
> ///////////////////////
> 
> setClass("A", representation(a = "numeric"))
> 
> setMethod("initialize", "A", function(.Object, a=1, ...)
> 
>                    cat("initialize in A\n")
>                .Object at a <- a
>                 return(.Object)
>               })
> 
> setClass("B", representation(b = "numeric"),contains=c("A"))
> 
> setMethod("initialize", "B", function(.Object, a=1,  b=2, ...)
> 
>             {
>               a.init <- b*b # or something more complexe ...
>               .Object <- callNextMethod(.Object,a=a.init, ...)
>               .Object at b <- b
>               return(.Object)
>             })
> 
> x <-new("B")
> 
> /////////////////// error with new("B").
> # The evaluation of "a=a.init" is done in the environment of initialize,A,
> and a.init doesn't exist
> # in this environment ...
> 
> initialize in A
> Error in .local(.Object, ...) : Object "a.init" not found
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc




More information about the R-help mailing list