Harold PETITHOMME
2010-Sep-23 14:21 UTC
[Rd] strange behaviour of callNextMethod in S4 methods
Hello,
I experienced a strange behaviour of callNextMethod when used in either
initialize or any other S4 function method definition.
Help says callNextMethod calls the next inherited method for the current
function from where it is called with the same actual (non missing)
arguments. This is OK. The problem appears when some formal arguments
(in particular, S4 objects) of this function are first used in the
current method and then must be used in the "next method" too. It
happens that such an argument is said to be missing in the "next
method". My guess is that using the argument in the current method makes
it disappear the next method. Is it a bug or a normal behaviour?
For understanding, let's see this example (with calls to some RNetCDF
functions):
Define a generic:
setGeneric("ncputVar",def=function(nc,variable,object)
standardGeneric("ncputVar"))
Define a 1st method for class Source:
setMethod("ncputVar",signature(nc="NetCDF",variable="ANY",object="Source"),
def=function(nc,variable,object)
{
att.put.nc(nc,variable,"nom","NC_CHAR",object at nom)
att.put.nc(nc,variable,"dom","NC_CHAR",object at dom)
att.put.nc(nc,variable,"loc","NC_CHAR",object at loc)
att.put.nc(nc,variable,"time","NC_CHAR",object at time)
}
)
Define an inherited method for class OPSource:
setMethod("ncputVar",signature(nc="NetCDF",variable="ANY",object="OPSource"),
def=function(nc,variable,object)
{
att.put.nc(nc,variable,"srctype","NC_CHAR","OPSource")
att.put.nc(nc,variable,"oporig","NC_CHAR",object at
oporig)
callNextMethod()
}
)
A call to ncputVar with an OPSource object causes an error saying object
is not present.
But if the method definition is changed like this:
setMethod("ncputVar",signature(nc="NetCDF",variable="ANY",object="OPSource"),
def=function(nc,variable,object)
{
o = object
att.put.nc(nc,variable,"srctype","NC_CHAR","OPSource")
att.put.nc(nc,variable,"oporig","NC_CHAR",object at
oporig)
callNextMethod(nc,variable,o)
}
)
there is no more an error.
Why does my "object" disappear?
Thanks to all.
Harold
--
*********************************************************
Harold PETITHOMME
Equipe Donn?es et Outils de Pr?vision (DPREVI/COMPAS/DOP)
M?t?o France - Direction de la Production
42, avenue G. Coriolis.
31057 Toulouse Cedex
France
Tel : (33/0)5.61.07.82.85
Fax : (33/0)5.61.07.86.09
E-mail : harold.petithomme at meteo.fr
Harold PETITHOMME <harold.petithomme at meteo.fr> writes: Hi,> > there is no more an error. > Why does my "object" disappear?I cannot reproduce your problem. Here is an example identical to yours: setClass("foo", list(a = "character", b = "numeric"), proto = 100, contains = "numeric") setClass("boo", list(c = "numeric"), contains = "foo") setGeneric("meth", def=function(variable, object, nc) standardGeneric(" setMethod("meth",signature(variable="ANY",object="numeric", nc="foo"), def=function(variable,object, nc) { nc at a <- as.character(variable) nc at b <- object+5 nc } ) meth( 2432, 22, new("foo")) setMethod("meth", signature(variable="ANY",object="numeric", nc="boo"), def=function(variable, object, nc) { nc at c <- object+10 callNextMethod() } ) meth(4232, 22, new("boo")) ##works ok Everything works as expected. Vitaly.> > Thanks to all. > Harold