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)
setMethod
documentation). The signature
argument to dumpMethods
is ignored (it
was used internally in previous implementations).
FALSE
to get only
non-generic functions. Usually omitted in calls to isGeneric
TRUE
, isGeneric
returns the name of
the generic. By default, it returns TRUE
. where
location).
removeMethods
, logical indicating if all
(default) or only the first method found should be removed.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"
).
isGeneric
:f
, and if so, is it a generic? The getName
argument allows a function to find the name
from a function definition. If it is TRUE
then the name of
the generic is returned, or FALSE
if this is not a generic
function definition. The behavior of isGeneric
and getGeneric
for
primitive functions is slightly different. These functions don't
exist as formal function objects (for efficiency and historical
reasons), regardless of whether methods have been defined for
them. A call to isGeneric
tells you whether methods have
been defined for this primitive function, anywhere in the current
search list, or in the specified position where
. In
contrast, a call to getGeneric
will return what the
generic for that function would be, even if no methods have been
currently defined for it.
removeGeneric
, removeMethods
:removeGeneric
removes the function
itself; removeMethods
restores the non-generic function
which was the default method. If there was no default method,
removeMethods
leaves a generic function with no methods.
standardGeneric
:f
. It is an error to call
standardGeneric
anywhere except in the body of the
corresponding generic function. Note that standardGeneric
is a primitive function in
the base package
for efficiency
reasons, but rather documented here where it belongs naturally.
dumpMethod
:findFunction
:name
exists. The returned value is always a
list, use the first element to access the first visible version
of the function. See the example. NOTE: Use this rather than find
with
mode="function"
, which is not as meaningful, and has a few
subtle bugs from its use of regular expressions. Also,
findFunction
works correctly in the code for a package
when attaching the package via a call to library
.
dumpMethods
:signature
:getGenerics
:where
; this
argument can be an environment or an index into the search
list. By default, the whole search list is used. The methods definitions are stored with
package qualifiers; for example, methods for function
"initialize"
might refer to two different functions
of that name, on different packages. The package names
corresponding to the method list object are contained in the
slot package
of the returned object. The form of
the returned name can be plain (e.g., "base"
), or in
the form used in the search list ("package:base"
)
according to the value of searchForm
setGeneric
:def
is supplied, and
the current function will become the default method for the
generic. If def
is supplied, this defines the generic function, and
no default method will exist (often a good feature, if the
function should only be available for a meaningful subset of all
objects). Arguments group
and valueClass
are retained for
consistency with S-Plus, but are currently not used.
isGeneric
:fdef
argument is supplied, take this as the
definition of the generic, and test whether it is really a
generic, with f
as the name of the generic. (This argument
is not available in S-Plus.)
removeGeneric
:where
supplied, just remove the version on this element
of the search list; otherwise, removes the first version
encountered.
standardGeneric
:standardGeneric
as their entire body. They can, however,
do any other computations as well. The usual setGeneric
(directly or through calling
setMethod
) creates a function with a call to
standardGeneric
.
dumpMethod
:findFunction
:generic
is FALSE
, ignore generic functions.
dumpMethods
:signature
is supplied only the methods matching this
initial signature are dumped. (This feature is not found in
S-Plus: don't use it if you want compatibility.)
signature
:signature
is to provide a check on
which arguments you meant, as well as clearer documentation in
your method specification. In addition, signature
checks
that each of the elements is a single character string.
removeMethods
:TRUE
if f
was a generic function,
FALSE
(silently) otherwise. If there is a default method, the function will be re-assigned as
a simple function with this definition.
Otherwise, the generic function remains but with no methods (so
any call to it will generate an error). In either case, a
following call to setMethod
will consistently
re-establish the same generic function as before.
Chambers, John M. (1998) Programming with Data Springer (For the original S4 version.)
getMethod
(also for selectMethod
),
setGeneric
,
setClass
,
showMethods
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)
## method dumping ------------------------------------
setClass("A", representation(a="numeric"))
setMethod("plot", "A", function(x,y,...){ cat("A meth\n") })
dumpMethod("plot","A", file="")
## Not run:
# setMethod("plot", "A",
# function (x, y, ...)
# {
# cat("AAAAA\n")
# }
# )
# ## End(Not run)
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 ...\n") })
dumpMethods("plot", file=tmp)
stopifnot(removeMethod("plot", "A"),
removeMethod("plot", "B"))
source(tmp)
stopifnot(is(getMethod("plot", "A"), "MethodDefinition"),
is(getMethod("plot", "B"), "MethodDefinition"))
Run the code above in your browser using DataLab