Tools for Managing Generic Functions

The functions documented here manage collections of methods associated with a generic function, as well as providing information about the generic functions themselves.

classes, methods, programming
isGeneric(f, where, fdef, getName = FALSE)
isGroup(f, where, fdef)
removeGeneric(f, where)

dumpMethod(f, signature, file, where, def) findFunction(f, generic = TRUE, where = topenv(parent.frame())) dumpMethods(f, file, signature, methods, where) signature(...)

removeMethods(f, where = topenv(parent.frame()), all = missing(where))

setReplaceMethod(f, ..., where = topenv(parent.frame()))

getGenerics(where, searchForm = FALSE)

The character string naming the function.
The environment, namespace, or search-list position from which to search for objects. By default, start at the top-level environment of the calling function, typically the global environment (i.e., use the search list), or the namespace of a package from which the call came. It is important to supply this argument when calling any of these functions indirectly. With package namespaces, the default is likely to be wrong in such calls.
The class signature of the relevant method. A signature is a named or unnamed vector of character strings. If named, the names must be formal argument names for the generic function. Signatures are matched to the arguments specified in the signature slot of the generic function (see the Details section of the setMethod documentation).

The signature argument to dumpMethods is ignored (it was used internally in previous implementations).

The file or connection on which to dump method definitions.
The function object defining the method; if omitted, the current method definition corresponding to the signature.
Named or unnamed arguments to form a signature.
In testing or finding functions, should generic functions be included. Supply as FALSE to get only non-generic functions.
Optional, the generic function definition.

Usually omitted in calls to isGeneric

If TRUE, isGeneric returns the name of the generic. By default, it returns TRUE.
The methods object containing the methods to be dumped. By default, the methods defined for this generic (optionally on the specified where location).
in removeMethods, logical indicating if all (default) or only the first method found should be removed.
In getGenerics, if TRUE, the package slot of the returned result is in the form used by search(), otherwise as the simple package name (e.g, "package:base" vs "base").

Chambers, John M. (2008) Software for Data Analysis: Programming with R Springer. (For the R version.)

Chambers, John M. (1998) Programming with Data Springer (For the original S4 version.)

See Also

getMethod (also for selectMethod), setGeneric, setClass, showMethods

  • GenericFunctions
  • isGeneric
  • isGroup
  • removeGeneric
  • getGenerics
  • dumpMethod
  • findFunction
  • dumpMethods
  • removeMethods
  • signature
  • setReplaceMethod
library(methods) require(stats) # for lm ## get the function "myFun" -- throw an error if 0 or > 1 versions visible: findFuncStrict <- function(fName) { allF <- findFunction(fName) if(length(allF) == 0) stop("No versions of ",fName,"visible") else if(length(allF) > 1) stop(fName,"is ambiguous: ", length(allF), "versions") else get(fName, allF[[1]]) } try(findFuncStrict("myFun"))# Error: no version lm <- function(x) x+1 try(findFuncStrict("lm"))# Error: 2 versions findFuncStrict("findFuncStrict")# just 1 version rm(lm) ## because nosegfault runs standardGeneric w/o the methods package, nothing ## really gets tested. The following check that it catches some errors mustDie <- function(expr) stopifnot(is(tryCatch(expr, error=function(e)e), "error")) mustDie(standardGeneric()) # 3 tests of requiring a single string mustDie(standardGeneric(NULL)) mustDie(standardGeneric("")) mustDie(standardGeneric("notAGenericFunction")) mustDie(standardGeneric("show")) # a generic, but not called from its body ## method dumping ------------------------------------ setClass("A", representation(a="numeric")) setMethod("plot", "A", function(x,y,...){ cat("A meth ") }) dumpMethod("plot","A", file="") setMethod("plot", "A", function (x, y, ...) { cat("AAAAA\n") } )tmp <- tempfile() dumpMethod("plot","A", file=tmp) ## now remove, and see if we can parse the dump stopifnot(removeMethod("plot", "A")) source(tmp) stopifnot(is(getMethod("plot", "A"), "MethodDefinition")) ## same with dumpMethods() : setClass("B", contains="A") setMethod("plot", "B", function(x,y,...){ cat("B ... ") }) dumpMethods("plot", file=tmp) stopifnot(removeMethod("plot", "A"), removeMethod("plot", "B")) source(tmp) stopifnot(is(getMethod("plot", "A"), "MethodDefinition"), is(getMethod("plot", "B"), "MethodDefinition"))
Documentation reproduced from package methods, version 3.3, License: Part of R @VERSION@

Community examples

Looks like there are no examples yet.