[R] setGeneric / standardGeneric when args are not "literals" - corrected

Vadim Ogranovich vograno at evafunds.com
Sat Jun 19 02:57:54 CEST 2004


This is a correction to my previous message, I forgot to swap two lines
in the body of setMakeGenericMethod. Sorry about that. The correct (full
message) reads like this:

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
because 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 necessary
  if (!isGeneric(methodName)) {
    if (is.function(methodName)) {
      fun.default <- get(methodName)
    }
    else {
      assign(methodName, methodName)

      browser()
      fun.default <- function(object) standardGeneric(methodName)
    }

    setGeneric(methodName, fun.default)
  }

  setMethod(methodName, className, fun)
}

Thanks,
Vadim




More information about the R-help mailing list