The R Project SVN R

Rev

Rev 59039 | Rev 71228 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/methods/man/GenericFunctions.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2007 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
17000 jmc 6
\name{GenericFunctions}
56186 murdoch 7
\alias{GenericFunctions}
17000 jmc 8
\alias{isGeneric}
9
\alias{isGroup}
10
\alias{removeGeneric}
11
\alias{getGenerics}
12
\alias{dumpMethod}
13
\alias{findFunction}
14
\alias{dumpMethods}
15
\alias{removeMethods}
16
\alias{signature}
17
\alias{setReplaceMethod}
33634 maechler 18
%% FIXME:  These are basically not documented at all:
45630 maechler 19
%% -----  isGroup, setReplaceMethod
17000 jmc 20
\title{Tools for Managing Generic Functions}
19029 hornik 21
\description{
22
  The functions documented here manage collections of methods associated
23
  with a generic function, as well as providing information about the
24
  generic functions themselves.
25
}
17000 jmc 26
\usage{
27
isGeneric(f, where, fdef, getName = FALSE)
28
isGroup(f, where, fdef)
29
removeGeneric(f, where)
30
 
31
dumpMethod(f, signature, file, where, def)
33635 maechler 32
findFunction(f, generic = TRUE, where = topenv(parent.frame()))
17000 jmc 33
dumpMethods(f, file, signature, methods, where)
17262 hornik 34
signature(\dots)
17000 jmc 35
 
56311 ripley 36
removeMethods(f, where = topenv(parent.frame()), all = missing(where))
56287 ripley 37
 
33635 maechler 38
setReplaceMethod(f, \dots, where = topenv(parent.frame()))
17000 jmc 39
 
20092 jmc 40
getGenerics(where, searchForm = FALSE)
17000 jmc 41
}
17211 jmc 42
\section{Summary of Functions}{
19029 hornik 43
  \describe{
31891 maechler 44
    \item{\code{isGeneric}:}{
19029 hornik 45
      Is there a function named \code{f}, and if so, is it a generic?
17211 jmc 46
 
19029 hornik 47
      The \code{getName} argument allows a function to find the name
48
      from a function definition.  If it is \code{TRUE} then the name of
49
      the generic is returned, or \code{FALSE} if this is not a generic
50
      function definition.
17000 jmc 51
 
19029 hornik 52
      The behavior of \code{isGeneric} and \code{\link{getGeneric}} for
53
      primitive functions is slightly different.  These functions don't
54
      exist as formal function objects (for efficiency and historical
55
      reasons), regardless of whether methods have been defined for
56
      them.  A call to \code{isGeneric} tells you whether methods have
33635 maechler 57
      been defined for this primitive function, anywhere in the current
58
      search list, or in the specified position \code{where}.  In
59
      contrast, a call to \code{\link{getGeneric}} will return what the
60
      generic for that function would be, even if no methods have been
61
      currently defined for it.
19029 hornik 62
    }
31891 maechler 63
 
64
    \item{\code{removeGeneric}, \code{removeMethods}:}{
65
      Remove all the methods for the generic function of this
20676 jmc 66
      name.  In addition, \code{removeGeneric} removes the function
67
      itself; \code{removeMethods} restores the non-generic function
68
      which was the default method.   If there was no default method,
69
      \code{removeMethods} leaves a generic function with no methods.
19029 hornik 70
    }
31891 maechler 71
    \item{\code{standardGeneric}:}{
19029 hornik 72
      Dispatches a method from the current function call for the generic
22294 jmc 73
      function \code{f}.  It is an error to call
74
      \code{standardGeneric} anywhere except in the body of the
75
      corresponding generic function.
33659 maechler 76
 
77
      Note that \code{\link{standardGeneric}} is a primitive function in
78
      the \pkg{base} package
79
      for efficiency % << MM: this is just my guess, what *are* the reasons?
80
      reasons, but rather documented here where it belongs naturally.
19029 hornik 81
    }
31891 maechler 82
    \item{\code{dumpMethod}:}{
83
      Dump the method for this generic function and signature.
19029 hornik 84
    }
31891 maechler 85
    \item{\code{findFunction}:}{
25351 jmc 86
      return a list of either the positions on the search list, or the
87
      current top-level environment, on which a function object
88
      for \code{name} exists.  The returned value is \emph{always} a
89
      list, use the first element to access the first visible version
90
      of the function.  See the example.
17000 jmc 91
 
19029 hornik 92
      \emph{NOTE:} Use this rather than \code{\link{find}} with
93
      \code{mode="function"}, which is not as meaningful, and has a few
25351 jmc 94
      subtle bugs from its use of regular expressions.  Also,
95
      \code{findFunction} works correctly in the code for a package
96
      when attaching the package via a call to \code{\link{library}}.
19029 hornik 97
    }
31891 maechler 98
    \item{\code{dumpMethods}:}{
99
      Dump all the methods for this generic.
19029 hornik 100
    }
31891 maechler 101
    \item{\code{signature}:}{
19029 hornik 102
      Returns a named list of classes to be matched to arguments of a
103
      generic function.
104
    }
45630 maechler 105
    \item{\code{getGenerics}:}{returns the names of the generic
33634 maechler 106
      functions that have methods defined on \code{where}; this
107
      argument can be an environment or an index into the search
108
      list.  By default, the whole search list is used.
20092 jmc 109
 
33634 maechler 110
      The methods definitions are stored with
111
      package qualifiers; for example, methods for function
112
      \code{"initialize"} might refer to two different functions
113
      of that name, on different packages.  The package names
114
      corresponding to the method list object are contained in the
115
      slot \code{package} of the returned object.  The form of
116
      the returned name can be plain (e.g., \code{"base"}), or in
117
      the form used in the search list (\code{"package:base"})
118
      according to the value of \code{searchForm}}
45824 jmc 119
 
17000 jmc 120
  }
121
}
122
 
123
\arguments{
124
  \item{f}{ The character string naming the function. }
56382 murdoch 125
  \item{where}{ The environment, namespace, or search-list position
26093 jmc 126
  from which to search for objects.  By default, start at the
127
  top-level environment of the calling function, typically the global
56382 murdoch 128
  environment (i.e., use the search list), or the namespace of a
26093 jmc 129
  package from which the call came.  It is important to supply this
130
  argument when calling any of these functions indirectly.  With
56382 murdoch 131
  package namespaces, the default is likely to be wrong in such calls.}
17000 jmc 132
 
133
  \item{signature}{ The class signature of the relevant method.  A
134
    signature is a named or unnamed vector of character strings.  If
135
    named, the names must be formal argument names for the generic
51304 jmc 136
    function.  Signatures are matched to the arguments specified in
137
    the signature slot of the generic function (see the Details
61433 ripley 138
    section of the \code{\link{setMethod}} documentation).
31891 maechler 139
 
51304 jmc 140
  The \code{signature} argument to \code{dumpMethods} is ignored (it
141
  was used internally in previous implementations).}
142
 
44809 maechler 143
  \item{file}{ The file or connection on which to dump method definitions. }
17000 jmc 144
 
145
  \item{def}{ The function object defining the method; if omitted, the
146
    current method definition corresponding to the signature. }
147
 
17262 hornik 148
  \item{\dots}{Named or unnamed arguments to form a signature.}
17000 jmc 149
 
150
  \item{generic}{In testing or finding functions, should generic
151
    functions be included.  Supply as \code{FALSE} to get only
152
    non-generic functions.}
153
 
154
  \item{fdef}{Optional, the generic function definition.
155
 
156
   Usually omitted in calls to \code{isGeneric}}
31891 maechler 157
 
17000 jmc 158
  \item{getName}{If \code{TRUE}, \code{isGeneric} returns the name of
159
    the generic.  By default, it returns \code{TRUE}. }
160
  \item{methods}{
161
    The methods object containing the methods to be dumped.  By default,
162
    the methods defined for this generic (optionally on the specified
163
    \code{where} location).
164
  }
33659 maechler 165
  \item{all}{in \code{removeMethods}, logical indicating if all
166
    (default) or only the first method found should be removed.}
33723 ripley 167
  \item{searchForm}{In \code{getGenerics}, if \code{TRUE}, the
168
    \code{package} slot of the returned result is in the form used
169
    by \code{search()}, otherwise as the simple package name (e.g,
170
    \code{"package:base"} vs \code{"base"}).
33659 maechler 171
  }
17000 jmc 172
}
173
 
17211 jmc 174
\section{Details}{
17000 jmc 175
  \describe{
31891 maechler 176
    \item{\code{setGeneric}:}{
19029 hornik 177
      If there is already a non-generic function of this name, it will
178
      be used to define the generic unless \code{def} is supplied, and
179
      the current function will become the default method for the
180
      generic.
17000 jmc 181
 
19029 hornik 182
      If \code{def} is supplied, this defines the generic function, and
183
      no default method will exist (often a good feature, if the
184
      function should only be available for a meaningful subset of all
185
      objects).
17000 jmc 186
 
19029 hornik 187
      Arguments \code{group} and \code{valueClass} are retained for
188
      consistency with S-Plus, but are currently not used.
189
    }
31891 maechler 190
    \item{\code{isGeneric}:}{
19029 hornik 191
      If the \code{fdef} argument is supplied, take this as the
192
      definition of the generic, and test whether it is really a
193
      generic, with \code{f} as the name of the generic.  (This argument
194
      is not available in S-Plus.)
195
    }
31891 maechler 196
    \item{\code{removeGeneric}:}{
19029 hornik 197
      If \code{where} supplied, just remove the version on this element
198
      of the search list; otherwise, removes the first version
199
      encountered.
200
    }
31891 maechler 201
    \item{\code{standardGeneric}:}{
19029 hornik 202
      Generic functions should usually have a call to
203
      \code{standardGeneric} as their entire body.  They can, however,
204
      do any other computations as well.
17000 jmc 205
 
19029 hornik 206
      The usual \code{setGeneric} (directly or through calling
207
      \code{setMethod}) creates a function with a call to
208
      \code{standardGeneric}.
209
    }
31891 maechler 210
    \item{\code{dumpMethod}:}{
19029 hornik 211
      The resulting source file will recreate the method.
212
    }
31891 maechler 213
    \item{\code{findFunction}:}{
19029 hornik 214
      If \code{generic} is \code{FALSE}, ignore generic functions.
215
    }
31891 maechler 216
    \item{\code{dumpMethods}:}{
19029 hornik 217
      If \code{signature} is supplied only the methods matching this
218
      initial signature are dumped.  (This feature is not found in
219
      S-Plus:  don't use it if you want compatibility.)
220
    }
31891 maechler 221
    \item{\code{signature}:}{
19029 hornik 222
      The advantage of using \code{signature} is to provide a check on
223
      which arguments you meant, as well as clearer documentation in
224
      your method specification.  In addition, \code{signature} checks
225
      that each of the elements is a single character string.
226
    }
31891 maechler 227
    \item{\code{removeMethods}:}{
19029 hornik 228
      Returns \code{TRUE} if \code{f} was a generic function,
229
      \code{FALSE} (silently) otherwise.
31891 maechler 230
 
19029 hornik 231
      If there is a default method, the function will be re-assigned as
20676 jmc 232
      a simple function with this definition.
233
      Otherwise, the generic function remains but with no methods (so
234
      any call to it will generate an error).  In either case, a
235
      following call to \code{setMethod} will consistently
236
      re-establish the same generic function as before.
19029 hornik 237
    }
17000 jmc 238
  }
239
}
240
\references{
45824 jmc 241
 Chambers, John M. (2008)
242
 \emph{Software for Data Analysis: Programming with R}
243
  Springer.  (For the R version.)
17000 jmc 244
 
45824 jmc 245
 Chambers, John M. (1998)
246
 \emph{Programming with Data}
247
 Springer (For the original S4 version.)
17000 jmc 248
}
19029 hornik 249
\seealso{
33634 maechler 250
  \code{\link{getMethod}} (also for \code{selectMethod}),
19029 hornik 251
  \code{\link{setGeneric}},
252
  \code{\link{setClass}},
253
  \code{\link{showMethods}}
254
}
22294 jmc 255
\examples{
41513 ripley 256
require(stats) # for lm
33659 maechler 257
 
258
## get the function "myFun" -- throw an error if 0 or > 1 versions visible:
259
findFuncStrict <- function(fName) {
260
  allF <- findFunction(fName)
25351 jmc 261
  if(length(allF) == 0)
33659 maechler 262
    stop("No versions of ",fName," visible")
25351 jmc 263
  else if(length(allF) > 1)
33659 maechler 264
    stop(fName," is ambiguous: ", length(allF), " versions")
25351 jmc 265
  else
33659 maechler 266
    get(fName, allF[[1]])
25351 jmc 267
}
31891 maechler 268
 
33663 maechler 269
try(findFuncStrict("myFun"))# Error: no version
33659 maechler 270
lm <- function(x) x+1
33663 maechler 271
try(findFuncStrict("lm"))#    Error: 2 versions
33662 maechler 272
findFuncStrict("findFuncStrict")# just 1 version
33663 maechler 273
rm(lm)
33659 maechler 274
 
26000 ripley 275
\dontshow{
22294 jmc 276
## because nosegfault runs standardGeneric w/o the methods package, nothing
277
## really gets tested.  The following check that it catches some errors
49719 maechler 278
mustDie <- function(expr)
279
   stopifnot(is(tryCatch(expr, error=function(e)e), "error"))
22294 jmc 280
 
281
mustDie(standardGeneric()) # 3 tests of requiring a single string
282
mustDie(standardGeneric(NULL))
283
mustDie(standardGeneric(""))
284
mustDie(standardGeneric("notAGenericFunction"))
285
mustDie(standardGeneric("show"))  # a generic, but not called from its body
286
}
44809 maechler 287
 
288
## method dumping ------------------------------------
289
 
290
setClass("A", representation(a="numeric"))
291
setMethod("plot", "A", function(x,y,...){ cat("A meth\n") })
292
dumpMethod("plot","A", file="")
293
\dontrun{
294
setMethod("plot", "A",
295
function (x, y, ...)
296
{
297
    cat("AAAAA\n")
22294 jmc 298
}
44809 maechler 299
)
300
}%dont
301
tmp <- tempfile()
302
dumpMethod("plot","A", file=tmp)
303
## now remove, and see if we can parse the dump
304
stopifnot(removeMethod("plot", "A"))
305
source(tmp)
306
stopifnot(is(getMethod("plot", "A"), "MethodDefinition"))
307
 
308
## same with dumpMethods() :
309
setClass("B", contains="A")
310
setMethod("plot", "B", function(x,y,...){ cat("B ...\n") })
311
dumpMethods("plot", file=tmp)
312
stopifnot(removeMethod("plot", "A"),
313
          removeMethod("plot", "B"))
314
source(tmp)
315
stopifnot(is(getMethod("plot", "A"), "MethodDefinition"),
316
          is(getMethod("plot", "B"), "MethodDefinition"))
317
}
17000 jmc 318
\keyword{programming}
319
\keyword{classes}
320
\keyword{methods}