| 42333 |
ripley |
1 |
% File src/library/methods/man/getMethod.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 70994 |
maechler |
3 |
% Copyright 1995-2016 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 15357 |
jmc |
6 |
\name{getMethod}
|
| 70994 |
maechler |
7 |
\title{Get or Test for the Definition of a Method}
|
| 56186 |
murdoch |
8 |
\alias{getMethod}
|
| 17454 |
jmc |
9 |
\alias{findMethod}
|
| 20092 |
jmc |
10 |
\alias{existsMethod}
|
| 15357 |
jmc |
11 |
\alias{selectMethod}
|
| 19473 |
jmc |
12 |
\alias{hasMethod}
|
| 15357 |
jmc |
13 |
\description{
|
| 71961 |
jmc |
14 |
The function \code{selectMethod()} returns the method that
|
|
|
15 |
would be selected for a call to function \code{f} if the arguments had
|
|
|
16 |
classes as specified by \code{signature}. Failing to find a method
|
|
|
17 |
is an error, unless argument \code{optional = TRUE}, in which case
|
|
|
18 |
\code{NULL} is returned.
|
| 45824 |
jmc |
19 |
|
| 71961 |
jmc |
20 |
The function \code{findMethod()} returns a list of
|
|
|
21 |
environments that contain a method for the specified function and signature; by
|
|
|
22 |
default, these are a subset of the packages in the current search
|
|
|
23 |
list. See section \dQuote{Using \code{findMethod()}} for details.
|
|
|
24 |
|
|
|
25 |
The function \code{getMethod()} returns the method corresponding to the
|
|
|
26 |
function and signature supplied similarly to \code{selectMethod}, but
|
|
|
27 |
without using inheritance or group generics.
|
|
|
28 |
|
|
|
29 |
The functions \code{hasMethod()} and
|
|
|
30 |
\code{existsMethod()} test whether \code{selectMethod()} or
|
|
|
31 |
\code{getMethod()}, respectively, finds a matching method.
|
|
|
32 |
|
|
|
33 |
|
| 15357 |
jmc |
34 |
}
|
|
|
35 |
\usage{
|
| 71961 |
jmc |
36 |
selectMethod(f, signature, optional = FALSE, useInherited =,
|
|
|
37 |
mlist = , fdef = , verbose = , doCache = )
|
| 45824 |
jmc |
38 |
|
| 70994 |
maechler |
39 |
findMethod(f, signature, where)
|
| 17454 |
jmc |
40 |
|
| 71961 |
jmc |
41 |
getMethod(f, signature = character(), where, optional = FALSE,
|
|
|
42 |
mlist, fdef)
|
| 70994 |
maechler |
43 |
|
| 71961 |
jmc |
44 |
existsMethod(f, signature = character(), where)
|
| 70994 |
maechler |
45 |
|
| 71961 |
jmc |
46 |
hasMethod(f, signature = character(), where)
|
| 15357 |
jmc |
47 |
}
|
|
|
48 |
\arguments{
|
| 70994 |
maechler |
49 |
\item{f}{a generic function or the character-string name of one.}
|
| 33634 |
maechler |
50 |
\item{signature}{the signature of classes to match to the arguments
|
| 70994 |
maechler |
51 |
of \code{f}. See the details below.}
|
| 71961 |
jmc |
52 |
\item{where}{the environment in which to look for the
|
|
|
53 |
method(s). By default, if the call comes from the command line, the table of methods defined in the generic
|
|
|
54 |
function itself is used, except for \code{findMethod} (see the
|
|
|
55 |
section below).}
|
| 18558 |
jmc |
56 |
|
| 70994 |
maechler |
57 |
\item{optional}{if the selection in \code{selectMethod} does not find
|
|
|
58 |
a valid method an error is generated, unless \code{optional} is
|
| 71961 |
jmc |
59 |
\code{TRUE}, in which case the value returned is \code{NULL}.}
|
|
|
60 |
|
| 70994 |
maechler |
61 |
\item{mlist, fdef, useInherited, verbose, doCache}{optional arguments
|
|
|
62 |
to \code{getMethod} and \code{selectMethod} for internal use. Avoid
|
|
|
63 |
these: some will work as expected and others will not, and none of
|
| 71961 |
jmc |
64 |
them is required for normal use of the functions. But see the
|
|
|
65 |
section \dQuote{Methods for \code{as()}} for nonstandard inheritance.}
|
| 15357 |
jmc |
66 |
}
|
| 71961 |
jmc |
67 |
\section{Using \code{findMethod()}}{
|
|
|
68 |
As its name suggests, this function is intended to behave like
|
|
|
69 |
\code{\link{find}}, which produces a list of the packages on the
|
|
|
70 |
current search list which have, and have exported, the object named.
|
|
|
71 |
That's what \code{findMethod} does also, by default. The
|
|
|
72 |
\dQuote{exported} part in this case means that the package's namespace
|
|
|
73 |
has an \code{exportMethods} directive for this generic function.
|
|
|
74 |
|
|
|
75 |
An important distinction is that the absence of such a directive does
|
|
|
76 |
not prevent methods from the package from being called once the
|
|
|
77 |
package is loaded. Otherwise, the code in the package could not use
|
|
|
78 |
un-exported methods.
|
|
|
79 |
|
|
|
80 |
So, if your question is whether loading package \code{thisPkg} will define a
|
|
|
81 |
method for this function and signature, you need to ask that question
|
|
|
82 |
about the namespace of the package:
|
|
|
83 |
|
|
|
84 |
\code{findMethod(f, signature, where = asNamespace("thisPkg"))}
|
|
|
85 |
|
|
|
86 |
If the package did not export the method, attaching it and calling
|
|
|
87 |
\code{findMethod} with no \code{where} argument will not find the
|
|
|
88 |
method.
|
|
|
89 |
|
|
|
90 |
Notice also that the length of the signature must be what the
|
|
|
91 |
corresponding package used. If \code{thisPkg} had only methods for
|
|
|
92 |
one argument, only length-1 signatures will match (no trailing
|
|
|
93 |
\code{"ANY"}), even if another currently loaded package had signatures
|
|
|
94 |
with more arguments.
|
|
|
95 |
}
|
|
|
96 |
\section{Methods for \code{as()}}{
|
|
|
97 |
The function \code{\link{setAs}} allows packages to define methods for
|
|
|
98 |
coercing one class of objects to another class. This works internally
|
|
|
99 |
by defining methods for the generic function \code{\link{coerce}(from,
|
|
|
100 |
to)},
|
|
|
101 |
which can not be called directly.
|
|
|
102 |
|
|
|
103 |
The \R evaluator selects
|
|
|
104 |
methods for this purpose using a different form of inheritance. While
|
|
|
105 |
methods can be inherited for the object being coerced, they cannot
|
|
|
106 |
inherit for the target class, since the result would not be a valid
|
|
|
107 |
object from that class.
|
|
|
108 |
If you want to
|
|
|
109 |
examine the selection procedure, you must supply the optional argument
|
|
|
110 |
\code{useInherited = c(TRUE, FALSE)} to \code{selectMethod}.
|
|
|
111 |
}
|
| 15357 |
jmc |
112 |
\details{
|
| 70994 |
maechler |
113 |
The \code{signature} argument specifies classes, corresponding to
|
|
|
114 |
formal arguments of the generic function; to be precise, to the
|
|
|
115 |
\code{signature} slot of the generic function object. The argument
|
|
|
116 |
may be a vector of strings identifying classes, and may be named or
|
|
|
117 |
not. Names, if supplied, match the names of those formal arguments
|
|
|
118 |
included in the signature of the generic. That signature is normally
|
|
|
119 |
all the arguments except \dots. However, generic functions can be
|
|
|
120 |
specified with only a subset of the arguments permitted, or with the
|
|
|
121 |
signature taking the arguments in a different order.
|
| 23839 |
jmc |
122 |
|
| 70994 |
maechler |
123 |
It's a good idea to name the arguments in the signature to avoid
|
|
|
124 |
confusion, if you're dealing with a generic that does something
|
|
|
125 |
special with its signature. In any case, the elements of the
|
|
|
126 |
signature are matched to the formal signature by the same rules used
|
|
|
127 |
in matching arguments in function calls (see
|
|
|
128 |
\code{\link{match.call}}).
|
| 23839 |
jmc |
129 |
|
| 70994 |
maechler |
130 |
The strings in the signature may be class names, \code{"missing"} or
|
| 71366 |
jmc |
131 |
\code{"ANY"}. See \link{Methods_Details} for the meaning of these in method
|
| 70994 |
maechler |
132 |
selection. Arguments not supplied in the signature implicitly
|
|
|
133 |
correspond to class \code{"ANY"}; in particular, giving an empty
|
|
|
134 |
signature means to look for the default method.
|
| 23839 |
jmc |
135 |
|
| 16802 |
jmc |
136 |
A call to \code{getMethod} returns the method for a particular
|
| 71961 |
jmc |
137 |
function and signature. The search for the method makes no use of
|
| 16802 |
jmc |
138 |
inheritance.
|
| 15357 |
jmc |
139 |
|
| 19473 |
jmc |
140 |
The function \code{selectMethod} also looks for a method given the
|
|
|
141 |
function and signature, but makes full use of the method dispatch
|
|
|
142 |
mechanism; i.e., inherited methods and group generics are taken into
|
|
|
143 |
account just as they would be in dispatching a method for the
|
|
|
144 |
corresponding signature, with the one exception that conditional
|
|
|
145 |
inheritance is not used. Like \code{getMethod}, \code{selectMethod}
|
|
|
146 |
returns \code{NULL} or generates an error if
|
|
|
147 |
the method is not found, depending on the argument \code{optional}.
|
|
|
148 |
|
| 71961 |
jmc |
149 |
Both \code{selectMethod} and \code{getMethod} will normally use the
|
|
|
150 |
current version of the generic function in the R session, which has a
|
|
|
151 |
table of the methods obtained from all the packages loaded in the
|
|
|
152 |
session. Optional arguments can cause a search for the generic function from a
|
|
|
153 |
specified environment, but this is rarely a useful idea. In contrast,
|
|
|
154 |
\code{findMethod} has a different default and the optional
|
|
|
155 |
\code{where=} argument may be needed. See the section \dQuote{Using
|
|
|
156 |
\code{findMethod()}}.
|
|
|
157 |
|
| 19473 |
jmc |
158 |
The functions \code{existsMethod} and \code{hasMethod} return
|
|
|
159 |
\code{TRUE} or \code{FALSE} according to whether a method is found,
|
|
|
160 |
the first corresponding to \code{getMethod} (no inheritance) and the
|
|
|
161 |
second to \code{selectMethod}.
|
|
|
162 |
|
| 15357 |
jmc |
163 |
}
|
|
|
164 |
\value{
|
| 45824 |
jmc |
165 |
The call to \code{selectMethod} or \code{getMethod} returns the selected method, if
|
|
|
166 |
one is found.
|
| 20092 |
jmc |
167 |
(This class extends \code{function}, so you can use the result
|
|
|
168 |
directly as a function if that is what you want.)
|
| 45824 |
jmc |
169 |
Otherwise an error is thrown if \code{optional} is \code{FALSE} and \code{NULL} is returned if
|
|
|
170 |
\code{optional} is \code{TRUE}.
|
| 15357 |
jmc |
171 |
|
| 45824 |
jmc |
172 |
The returned method object is a
|
|
|
173 |
\code{\linkS4class{MethodDefinition}} object, \emph{except} that the default method for a primitive function is required to be the primitive itself.
|
| 71961 |
jmc |
174 |
Note therefore that the only reliable test that the search failed is
|
|
|
175 |
\code{is.null()}.
|
| 45824 |
jmc |
176 |
|
| 71961 |
jmc |
177 |
The returned value of \code{findMethod} is a list of
|
|
|
178 |
environments in which a corresponding method was found; that is, a
|
|
|
179 |
table of methods including the one specified.
|
|
|
180 |
|
| 15357 |
jmc |
181 |
}
|
|
|
182 |
\references{
|
| 88585 |
hornik |
183 |
\bibshow{R:Chambers:2016}
|
|
|
184 |
(Chapters 9 and 10.)
|
| 71961 |
jmc |
185 |
|
| 88589 |
hornik |
186 |
\bibshow{R:Chambers:2008}
|
|
|
187 |
(Section 10.6 for some details of method selection.)
|
| 15357 |
jmc |
188 |
}
|
| 88589 |
hornik |
189 |
\seealso{
|
|
|
190 |
\code{\link{Methods_Details}} for the details of method selection;
|
|
|
191 |
\code{\link{GenericFunctions}} for other functions manipulating
|
|
|
192 |
methods and generic function objects;
|
| 45824 |
jmc |
193 |
\code{\linkS4class{MethodDefinition}} for the class that represents
|
| 88589 |
hornik |
194 |
method definitions.
|
|
|
195 |
}
|
| 26477 |
jmc |
196 |
\examples{
|
| 71961 |
jmc |
197 |
testFun <- function(x)x
|
|
|
198 |
setGeneric("testFun")
|
| 26477 |
jmc |
199 |
setMethod("testFun", "numeric", function(x)x+1)
|
| 71963 |
jmc |
200 |
|
|
|
201 |
hasMethod("testFun", "numeric") # TRUE
|
|
|
202 |
|
|
|
203 |
hasMethod("testFun", "integer") #TRUE, inherited
|
|
|
204 |
|
|
|
205 |
existsMethod("testFun", "integer") #FALSE
|
|
|
206 |
|
|
|
207 |
hasMethod("testFun") # TRUE, default method
|
|
|
208 |
|
| 26477 |
jmc |
209 |
hasMethod("testFun", "ANY")
|
| 71963 |
jmc |
210 |
|
| 26477 |
jmc |
211 |
\dontshow{
|
| 71963 |
jmc |
212 |
## Verify the example
|
| 33634 |
maechler |
213 |
stopifnot(isGeneric("testFun"),
|
| 47616 |
ripley |
214 |
hasMethod("testFun", "numeric"),
|
|
|
215 |
hasMethod("testFun", "integer"),
|
|
|
216 |
!existsMethod("testFun", "integer"),
|
| 71961 |
jmc |
217 |
hasMethod("testFun"),
|
|
|
218 |
hasMethod("testFun", "ANY") )
|
| 26477 |
jmc |
219 |
removeGeneric("testFun")
|
|
|
220 |
}
|
|
|
221 |
}
|
| 15357 |
jmc |
222 |
\keyword{programming}
|
|
|
223 |
\keyword{classes}
|
|
|
224 |
\keyword{methods}
|