| Line 87... |
Line 87... |
| 87 |
The most common examples arise with binary operators. Methods may be
|
87 |
The most common examples arise with binary operators. Methods may be
|
| 88 |
defined for individual operators, for special groups of operators such as
|
88 |
defined for individual operators, for special groups of operators such as
|
| 89 |
\code{\link{Arith}} or for group \code{\link{Ops}}.
|
89 |
\code{\link{Arith}} or for group \code{\link{Ops}}.
|
| 90 |
|
90 |
|
| 91 |
}
|
91 |
}
|
| - |
|
92 |
\section{Exporting Methods}{
|
| - |
|
93 |
If a package defines methods for generic functions, those methods
|
| - |
|
94 |
should be exported if any of the classes involved are exported; in
|
| - |
|
95 |
other words, if someone using the package might expect these methods
|
| - |
|
96 |
to be called.
|
| - |
|
97 |
Methods are exported by including an \code{exportMethods()} directive
|
| - |
|
98 |
in the \code{NAMESPACE} file for the package, with the arguments to
|
| - |
|
99 |
the directive being the names of the generic functions for which
|
| - |
|
100 |
methods have been defined.
|
| - |
|
101 |
|
| - |
|
102 |
Exporting methods is always desirable in the sense of declaring what
|
| - |
|
103 |
you want to happen, in that you do expect users to find such methods.
|
| - |
|
104 |
It can be essential in the case that the method was defined for a
|
| - |
|
105 |
function that is not originally a generic function in its own package
|
| - |
|
106 |
(for example, \code{plot()} in the \code{graphics} package). In this
|
| - |
|
107 |
case it may be that the version of the function in the \R session is
|
| - |
|
108 |
not generic, and your methods will not be called.
|
| - |
|
109 |
|
| - |
|
110 |
Exporting methods for a function also exports the generic version of
|
| - |
|
111 |
the function.
|
| - |
|
112 |
Keep in mind that this does \emph{not} conflict with the function as
|
| - |
|
113 |
it was originally defined in another package; on the contrary, it's
|
| - |
|
114 |
designed to ensure that the function in the \R session dispatches
|
| - |
|
115 |
methods correctly for your classes and continues to behave as expected
|
| - |
|
116 |
when no specific methods apply. See \link{Methods_Details} for the actual mechanism.
|
| - |
|
117 |
}
|
| 92 |
\section{Details}{
|
118 |
\section{Details}{
|
| 93 |
The call to \code{setMethod} stores the supplied method definition in
|
119 |
The call to \code{setMethod} stores the supplied method definition in
|
| 94 |
the metadata table for this generic function in the environment,
|
120 |
the metadata table for this generic function in the environment,
|
| 95 |
typically the global environment or the namespace of a package.
|
121 |
typically the global environment or the namespace of a package.
|
| 96 |
In the case of a package, the table object becomes part of the namespace or environment of the
|
122 |
In the case of a package, the table object becomes part of the namespace or environment of the
|