[R] setGeneric / standardGeneric when args are not "literals"
Vadim Ogranovich
vograno at evafunds.com
Sat Jun 19 02:48:36 CEST 2004
Hi,
This works
> setGeneric("clear", function(obj) standardGeneric("clear"))
[1] "clear"
but this doesn't. Why?
> funName <- "clear"
> setGeneric(funName, function(obj) standardGeneric(funName))
Error in .recursiveCallTest(body, fname) :
(converted from warning) The body of the generic function for "clear"
calls standardGeneric to dispatch on a different name ("funName")!
This is R-1.8.1 on RH-7.3
I came across it while trying to write a helper function that would
"safely" create generics when a function with such a name already
exists. Here is what I adapted from S4Objects but it doesn't work
becuase of the above-mentioned problem. Any suggestion how to make it
work, please?
setMakeGenericMethod <- function(methodName, className, fun) {
# sets a method and creates the generics if neccessary
if (!isGeneric(methodName)) {
if (is.function(methodName)) {
fun.default <- get(methodName)
}
else {
fun.default <- function(object) standardGeneric(methodName)
}
}
setGeneric(methodName, fun.default)
setMethod(methodName, className, fun)
}
Thanks,
Vadim
More information about the R-help
mailing list