[R] extension of generic functions in methods package

Thomas Koenig thomasio at cs.tu-berlin.de
Mon Feb 3 20:50:22 CET 2003


How can I extend the signature of a generic function, because the following 
example is unclear for me.

setClass("A",representation(data="numeric"));

setGeneric("func",function(obj,...){
  res <- standardGeneric("func");
  res at cal <- match.arg();
  return(res);
});

func1.A <- function(obj){
  print("A");
  return();
}

setMethod("func","A",func1.A);
showMethods("func")

##Function "func":
##obj = "A"

func2.A <- function(obj,x){
  print("A");
  return();
}

## gives error
## setMethod("func",c("A","numeric"),func2.A);
##Error in matchSignature(signature, fdef) :
##       Invalid names in signature:

## extending the signature
setGeneric("func",function(obj,x,...){
  res <- standardGeneric("func");
  res at cal <- match.arg();
  return(res);
});

setMethod("func",c("A","numeric"),func2.A);

## but:
showMethods("func")

##Function "func":
##obj = "A", x = "numeric"

## and
a <- new("A");
func(A)
## gives
##Error in func(a) : No direct or inherited method for function "func" for 
##this call

## but this works
func(a,1)

##[1] "A"
##NULL


My questions:
1) Is it correct, that func1.A disappears in the showMethods output?
2) How can I extend the signature of a generic function, without removing the  
other functions?

Thanks

Thomas




More information about the R-help mailing list