Rev 68017 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/getMethod.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2012 R Core Team% Distributed under GPL 2 or later\name{getMethod}\alias{getMethod}\alias{findMethod}\alias{existsMethod}\alias{selectMethod}\alias{hasMethod}\title{ Get or Test for the Definition of a Method }\description{Functions to look for a method corresponding to a given generic function and signature.The functions \code{getMethod} and \code{selectMethod} return the method; the functions\code{existsMethod} and \code{hasMethod} test for its existence. In bothcases the first function only gets direct definitions and the seconduses inheritance. In all cases, the search is in the generic function itself or inthe package/environment specified by argument \code{where}.The function \code{findMethod} returns the package(s) in the searchlist (or in the packages specified by the \code{where} argument) thatcontain a method for this function and signature.}\usage{getMethod(f, signature=character(), where, optional = FALSE,mlist, fdef)existsMethod(f, signature = character(), where)findMethod(f, signature, where)selectMethod(f, signature, optional = FALSE, useInherited =,mlist = , fdef = , verbose = , doCache = )hasMethod(f, signature=character(), where)}\arguments{\item{f}{A generic function or the character-string name of one.}\item{signature}{the signature of classes to match to the argumentsof \code{f}. See the details below.}\item{where}{The position or environment in which to look for themethod(s): by default, the table of methods defined in the generic function itself is used.}\item{optional}{If the selection in \code{selectMethod} does not find a valid methodan error is generated, unless this argument is \code{TRUE}. In thatcase, the value returned is \code{NULL} if no method matches.}\item{mlist, fdef, useInherited, verbose, doCache}{Optionalarguments to \code{getMethod} and \code{selectMethod} forinternal use. Avoidthese: some will work as expected and others will not, and none of them is required for normal use of the functions.}}\details{The \code{signature} argument specifies classes, corresponding to formal arguments of the generic function; to be precise, to the \code{signature} slot of the generic function object.The argument may be a vector of strings identifyingclasses, and may be named or not. Names, if supplied, match thenames of those formal arguments included in the signature of thegeneric. That signature is normally all the arguments except\dots. However, generic functions can be specified with only asubset of the arguments permitted, or with the signature takingthe arguments in a different order.It's a good idea to name the arguments in the signature to avoidconfusion, if you're dealing with a generic that does somethingspecial with its signature. In any case, the elements of thesignature are matched to the formal signature by the same rulesused in matching arguments in function calls (see\code{\link{match.call}}).The strings in the signature may be class names, \code{"missing"}or \code{"ANY"}. See \link{Methods} for the meaning of these inmethod selection. Arguments not supplied in the signatureimplicitly correspond to class \code{"ANY"}; in particular, givingan empty signature means to look for the default method.A call to \code{getMethod} returns the method for a particularfunction and signature. As with other \code{get} functions,argument \code{where} controls where the function looks (by defaultanywhere in the search list) and argument \code{optional} controlswhether the function returns \code{NULL} or generates an error ifthe method is not found. The search for the method makes no use ofinheritance.The function \code{selectMethod} also looks for a method given thefunction and signature, but makes full use of the method dispatchmechanism; i.e., inherited methods and group generics are taken intoaccount just as they would be in dispatching a method for thecorresponding signature, with the one exception that conditionalinheritance is not used. Like \code{getMethod}, \code{selectMethod}returns \code{NULL} or generates an error ifthe method is not found, depending on the argument \code{optional}.The functions \code{existsMethod} and \code{hasMethod} return\code{TRUE} or \code{FALSE} according to whether a method is found,the first corresponding to \code{getMethod} (no inheritance) and thesecond to \code{selectMethod}.}\value{The call to \code{selectMethod} or \code{getMethod} returns the selected method, ifone is found.(This class extends \code{function}, so you can use the resultdirectly as a function if that is what you want.)Otherwise an error is thrown if \code{optional} is \code{FALSE} and \code{NULL} is returned if\code{optional} is \code{TRUE}.The returned method object is a\code{\linkS4class{MethodDefinition}} object, \emph{except} that the default method for a primitive function is required to be the primitive itself.Note therefore that the only reliable test that the search failed is \code{is.null()}.}\references{Chambers, John M. (2008)\emph{Software for Data Analysis: Programming with R}Springer. (For the R version.)Chambers, John M. (1998)\emph{Programming with Data}Springer (For the original S4 version.)}\seealso{\code{\link{Methods}} for the details of methodselection; \code{\link{GenericFunctions}} for other functionsmanipulating methods and generic function objects;\code{\linkS4class{MethodDefinition}} for the class that representsmethod definitions.}\examples{setGeneric("testFun", function(x)standardGeneric("testFun"))setMethod("testFun", "numeric", function(x)x+1)hasMethod("testFun", "numeric")\dontrun{[1] TRUE}hasMethod("testFun", "integer") #inherited\dontrun{[1] TRUE}existsMethod("testFun", "integer")\dontrun{[1] FALSE}hasMethod("testFun") # default method\dontrun{[1] FALSE}hasMethod("testFun", "ANY")\dontrun{[1] FALSE}\dontshow{stopifnot(isGeneric("testFun"),hasMethod("testFun", "numeric"),hasMethod("testFun", "integer"),!existsMethod("testFun", "integer"),!hasMethod("testFun"),!hasMethod("testFun", "ANY") )removeGeneric("testFun")}}\keyword{programming}\keyword{classes}\keyword{methods}