| Line 9... |
Line 9... |
| 9 |
\alias{findMethod}
|
9 |
\alias{findMethod}
|
| 10 |
\alias{existsMethod}
|
10 |
\alias{existsMethod}
|
| 11 |
\alias{selectMethod}
|
11 |
\alias{selectMethod}
|
| 12 |
\alias{hasMethod}
|
12 |
\alias{hasMethod}
|
| 13 |
\description{
|
13 |
\description{
|
| - |
|
14 |
The function \code{selectMethod()} returns the method that
|
| 14 |
Functions to look for a method corresponding to a given generic function and signature.
|
15 |
would be selected for a call to function \code{f} if the arguments had
|
| 15 |
The functions \code{getMethod} and \code{selectMethod} return the method; the functions
|
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.
|
| - |
|
19 |
|
| 16 |
\code{existsMethod} and \code{hasMethod} test for its existence. In both
|
20 |
The function \code{findMethod()} returns a list of
|
| 17 |
cases the first function only gets direct definitions and the second
|
21 |
environments that contain a method for the specified function and signature; by
|
| 18 |
uses inheritance. In all cases, the search is in the generic function itself or in
|
22 |
default, these are a subset of the packages in the current search
|
| 19 |
the package/environment specified by argument \code{where}.
|
23 |
list. See section \dQuote{Using \code{findMethod()}} for details.
|
| 20 |
|
24 |
|
| 21 |
The function \code{findMethod} returns the package(s) in the search
|
25 |
The function \code{getMethod()} returns the method corresponding to the
|
| 22 |
list (or in the packages specified by the \code{where} argument) that
|
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
|
| 23 |
contain a method for this function and signature.
|
31 |
\code{getMethod()}, respectively, finds a matching method.
|
| - |
|
32 |
|
| - |
|
33 |
|
| 24 |
}
|
34 |
}
|
| 25 |
\usage{
|
35 |
\usage{
|
| 26 |
existsMethod(f, signature = character(), where)
|
36 |
selectMethod(f, signature, optional = FALSE, useInherited =,
|
| - |
|
37 |
mlist = , fdef = , verbose = , doCache = )
|
| 27 |
|
38 |
|
| 28 |
findMethod(f, signature, where)
|
39 |
findMethod(f, signature, where)
|
| 29 |
|
40 |
|
| 30 |
getMethod(f, signature = character(), where, optional = FALSE, mlist, fdef)
|
41 |
getMethod(f, signature = character(), where, optional = FALSE,
|
| - |
|
42 |
mlist, fdef)
|
| 31 |
|
43 |
|
| 32 |
hasMethod(f, signature = character(), where)
|
44 |
existsMethod(f, signature = character(), where)
|
| 33 |
|
45 |
|
| 34 |
selectMethod(f, signature, optional = FALSE, useInherited =,
|
46 |
hasMethod(f, signature = character(), where)
|
| 35 |
mlist = , fdef = , verbose = , doCache = )
|
- |
|
| 36 |
}
|
47 |
}
|
| 37 |
\arguments{
|
48 |
\arguments{
|
| 38 |
\item{f}{a generic function or the character-string name of one.}
|
49 |
\item{f}{a generic function or the character-string name of one.}
|
| 39 |
\item{signature}{the signature of classes to match to the arguments
|
50 |
\item{signature}{the signature of classes to match to the arguments
|
| 40 |
of \code{f}. See the details below.}
|
51 |
of \code{f}. See the details below.}
|
| 41 |
\item{where}{the position or environment in which to look for the
|
52 |
\item{where}{the environment in which to look for the
|
| 42 |
method(s): by default, the table of methods defined in the generic
|
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
|
| 43 |
function itself is used.}
|
55 |
section below).}
|
| 44 |
|
56 |
|
| 45 |
\item{optional}{if the selection in \code{selectMethod} does not find
|
57 |
\item{optional}{if the selection in \code{selectMethod} does not find
|
| 46 |
a valid method an error is generated, unless \code{optional} is
|
58 |
a valid method an error is generated, unless \code{optional} is
|
| 47 |
true. In that case, the value returned is \code{NULL} if no method
|
59 |
\code{TRUE}, in which case the value returned is \code{NULL}.}
|
| 48 |
matches.}
|
60 |
|
| 49 |
\item{mlist, fdef, useInherited, verbose, doCache}{optional arguments
|
61 |
\item{mlist, fdef, useInherited, verbose, doCache}{optional arguments
|
| 50 |
to \code{getMethod} and \code{selectMethod} for internal use. Avoid
|
62 |
to \code{getMethod} and \code{selectMethod} for internal use. Avoid
|
| 51 |
these: some will work as expected and others will not, and none of
|
63 |
these: some will work as expected and others will not, and none of
|
| 52 |
them is required for normal use of the functions.}
|
64 |
them is required for normal use of the functions. But see the
|
| - |
|
65 |
section \dQuote{Methods for \code{as()}} for nonstandard inheritance.}
|
| - |
|
66 |
}
|
| - |
|
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}.
|
| 53 |
}
|
111 |
}
|
| 54 |
\details{
|
112 |
\details{
|
| 55 |
The \code{signature} argument specifies classes, corresponding to
|
113 |
The \code{signature} argument specifies classes, corresponding to
|
| 56 |
formal arguments of the generic function; to be precise, to the
|
114 |
formal arguments of the generic function; to be precise, to the
|
| 57 |
\code{signature} slot of the generic function object. The argument
|
115 |
\code{signature} slot of the generic function object. The argument
|
| Line 74... |
Line 132... |
| 74 |
selection. Arguments not supplied in the signature implicitly
|
132 |
selection. Arguments not supplied in the signature implicitly
|
| 75 |
correspond to class \code{"ANY"}; in particular, giving an empty
|
133 |
correspond to class \code{"ANY"}; in particular, giving an empty
|
| 76 |
signature means to look for the default method.
|
134 |
signature means to look for the default method.
|
| 77 |
|
135 |
|
| 78 |
A call to \code{getMethod} returns the method for a particular
|
136 |
A call to \code{getMethod} returns the method for a particular
|
| 79 |
function and signature. As with other \code{get} functions,
|
- |
|
| 80 |
argument \code{where} controls where the function looks (by default
|
- |
|
| 81 |
anywhere in the search list) and argument \code{optional} controls
|
- |
|
| 82 |
whether the function returns \code{NULL} or generates an error if
|
- |
|
| 83 |
the method is not found. The search for the method makes no use of
|
137 |
function and signature. The search for the method makes no use of
|
| 84 |
inheritance.
|
138 |
inheritance.
|
| 85 |
|
139 |
|
| 86 |
The function \code{selectMethod} also looks for a method given the
|
140 |
The function \code{selectMethod} also looks for a method given the
|
| 87 |
function and signature, but makes full use of the method dispatch
|
141 |
function and signature, but makes full use of the method dispatch
|
| 88 |
mechanism; i.e., inherited methods and group generics are taken into
|
142 |
mechanism; i.e., inherited methods and group generics are taken into
|
| Line 90... |
Line 144... |
| 90 |
corresponding signature, with the one exception that conditional
|
144 |
corresponding signature, with the one exception that conditional
|
| 91 |
inheritance is not used. Like \code{getMethod}, \code{selectMethod}
|
145 |
inheritance is not used. Like \code{getMethod}, \code{selectMethod}
|
| 92 |
returns \code{NULL} or generates an error if
|
146 |
returns \code{NULL} or generates an error if
|
| 93 |
the method is not found, depending on the argument \code{optional}.
|
147 |
the method is not found, depending on the argument \code{optional}.
|
| 94 |
|
148 |
|
| - |
|
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 |
|
| 95 |
The functions \code{existsMethod} and \code{hasMethod} return
|
158 |
The functions \code{existsMethod} and \code{hasMethod} return
|
| 96 |
\code{TRUE} or \code{FALSE} according to whether a method is found,
|
159 |
\code{TRUE} or \code{FALSE} according to whether a method is found,
|
| 97 |
the first corresponding to \code{getMethod} (no inheritance) and the
|
160 |
the first corresponding to \code{getMethod} (no inheritance) and the
|
| 98 |
second to \code{selectMethod}.
|
161 |
second to \code{selectMethod}.
|
| 99 |
|
162 |
|
| Line 106... |
Line 169... |
| 106 |
Otherwise an error is thrown if \code{optional} is \code{FALSE} and \code{NULL} is returned if
|
169 |
Otherwise an error is thrown if \code{optional} is \code{FALSE} and \code{NULL} is returned if
|
| 107 |
\code{optional} is \code{TRUE}.
|
170 |
\code{optional} is \code{TRUE}.
|
| 108 |
|
171 |
|
| 109 |
The returned method object is a
|
172 |
The returned method object is a
|
| 110 |
\code{\linkS4class{MethodDefinition}} object, \emph{except} that the default method for a primitive function is required to be the primitive itself.
|
173 |
\code{\linkS4class{MethodDefinition}} object, \emph{except} that the default method for a primitive function is required to be the primitive itself.
|
| 111 |
Note therefore that the only reliable test that the search failed is \code{is.null()}.
|
174 |
Note therefore that the only reliable test that the search failed is
|
| - |
|
175 |
\code{is.null()}.
|
| - |
|
176 |
|
| - |
|
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.
|
| 112 |
|
180 |
|
| 113 |
}
|
181 |
}
|
| 114 |
\references{
|
182 |
\references{
|
| - |
|
183 |
Chambers, John M. (2016)
|
| - |
|
184 |
\emph{Extending R},
|
| - |
|
185 |
Chapman & Hall.
|
| - |
|
186 |
(Chapters 9 and 10.)
|
| - |
|
187 |
|
| 115 |
Chambers, John M. (2008)
|
188 |
Chambers, John M. (2008)
|
| 116 |
\emph{Software for Data Analysis: Programming with R}
|
189 |
\emph{Software for Data Analysis: Programming with R}
|
| 117 |
Springer. (For the R version.)
|
- |
|
| 118 |
|
- |
|
| 119 |
Chambers, John M. (1998)
|
- |
|
| 120 |
\emph{Programming with Data}
|
- |
|
| 121 |
Springer (For the original S4 version.)
|
190 |
Springer. (Section 10.6 for some details of method selection.)
|
| 122 |
}
|
191 |
}
|
| 123 |
\seealso{\code{\link{Methods_Details}} for the details of method
|
192 |
\seealso{\code{\link{Methods_Details}} for the details of method
|
| 124 |
selection; \code{\link{GenericFunctions}} for other functions
|
193 |
selection; \code{\link{GenericFunctions}} for other functions
|
| 125 |
manipulating methods and generic function objects;
|
194 |
manipulating methods and generic function objects;
|
| 126 |
\code{\linkS4class{MethodDefinition}} for the class that represents
|
195 |
\code{\linkS4class{MethodDefinition}} for the class that represents
|
| 127 |
method definitions.}
|
196 |
method definitions.}
|
| 128 |
\examples{
|
197 |
\examples{
|
| - |
|
198 |
testFun <- function(x)x
|
| 129 |
setGeneric("testFun", function(x)standardGeneric("testFun"))
|
199 |
setGeneric("testFun")
|
| 130 |
setMethod("testFun", "numeric", function(x)x+1)
|
200 |
setMethod("testFun", "numeric", function(x)x+1)
|
| 131 |
hasMethod("testFun", "numeric")
|
201 |
hasMethod("testFun", "numeric")
|
| 132 |
\dontrun{[1] TRUE}
|
202 |
\dontrun{[1] TRUE}
|
| 133 |
hasMethod("testFun", "integer") #inherited
|
203 |
hasMethod("testFun", "integer") #inherited
|
| 134 |
\dontrun{[1] TRUE}
|
204 |
\dontrun{[1] TRUE}
|
| 135 |
existsMethod("testFun", "integer")
|
205 |
existsMethod("testFun", "integer")
|
| 136 |
\dontrun{[1] FALSE}
|
206 |
\dontrun{[1] FALSE}
|
| 137 |
hasMethod("testFun") # default method
|
207 |
hasMethod("testFun") # default method
|
| 138 |
\dontrun{[1] FALSE}
|
208 |
\dontrun{[1] TRUE}
|
| 139 |
hasMethod("testFun", "ANY")
|
209 |
hasMethod("testFun", "ANY")
|
| 140 |
\dontrun{[1] FALSE}
|
210 |
\dontrun{[1] FALSE}
|
| 141 |
\dontshow{
|
211 |
\dontshow{
|
| 142 |
stopifnot(isGeneric("testFun"),
|
212 |
stopifnot(isGeneric("testFun"),
|
| 143 |
hasMethod("testFun", "numeric"),
|
213 |
hasMethod("testFun", "numeric"),
|
| 144 |
hasMethod("testFun", "integer"),
|
214 |
hasMethod("testFun", "integer"),
|
| 145 |
!existsMethod("testFun", "integer"),
|
215 |
!existsMethod("testFun", "integer"),
|
| 146 |
!hasMethod("testFun"),
|
216 |
hasMethod("testFun"),
|
| 147 |
!hasMethod("testFun", "ANY") )
|
217 |
hasMethod("testFun", "ANY") )
|
| 148 |
removeGeneric("testFun")
|
218 |
removeGeneric("testFun")
|
| 149 |
}
|
219 |
}
|
| 150 |
}
|
220 |
}
|
| 151 |
\keyword{programming}
|
221 |
\keyword{programming}
|
| 152 |
\keyword{classes}
|
222 |
\keyword{classes}
|