The R Project SVN R

Rev

Rev 88585 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/utils/man/methods.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88539 hornik 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{methods}
7
\title{List Methods for S3 Generic Functions or Classes}
67997 morgan 8
\alias{.S3methods}
56186 murdoch 9
\alias{methods}
77517 maechler 10
\alias{format.MethodsFunction}
27442 ripley 11
\alias{print.MethodsFunction}
12
\description{
67997 morgan 13
  List all available methods for a S3 and S4 generic function, or all
14
  methods for an S3 or S4 class.
27442 ripley 15
}
84495 maechler 16
\usage{% --> ../R/objects.R :
17
   methods(generic.function, class, all.names = FALSE, dropPath = FALSE)
18
.S3methods(generic.function, class, envir = parent.frame(),
86898 maechler 19
                                    all.names = FALSE, dropPath = FALSE, useEnv = FALSE)
68162 maechler 20
 
77517 maechler 21
\S3method{format}{MethodsFunction}(x, byclass = attr(x, "byclass"), \dots)
84496 maechler 22
\S3method{print}{MethodsFunction}(x, byclass = attr(x, "byclass"), \dots)
27442 ripley 23
}
24
\arguments{
25
  \item{generic.function}{a generic function, or a character string naming a
26
    generic function.}
27
  \item{class}{a symbol or character string naming a class: only used if
28
    \code{generic.function} is not supplied.}
68001 morgan 29
  \item{envir}{the environment in which to look for the definition of
30
    the generic function, when the generic function is passed as a
31
    character string.}
84397 maechler 32
  \item{all.names}{a \code{\link{logical}} indicating if all
33
    object names are returned.  When \code{FALSE} as by default, names
34
    beginning with a \samp{.} are omitted.}
84495 maechler 35
  \item{dropPath}{a \code{\link{logical}} indicating if the
36
    \code{\link{search}()} path, apart from \code{\link{.GlobalEnv}} and
37
    \code{package:base} (i.e., \code{\link{baseenv}()}), should be skipped
38
    when searching for method definitions.  The default \code{FALSE} is
85953 hornik 39
    back compatible and typically desired for \I{\code{print()}ing}, with or
84495 maechler 40
    without asterisk; \code{dropPath=TRUE} has been hard coded in \R 4.3.0
41
    and is faster for non-small \code{\link{search}()} paths.}
86898 maechler 42
  \item{useEnv}{a \code{\link{logical}} indicating if \code{envir} should
43
    be searched in addition to the (much reduced in case of \code{dropPath=TRUE})
44
    \code{\link{search}()} path.}
77517 maechler 45
  \item{x}{typically the result of \code{methods(..)}, an \R object of S3
68162 maechler 46
    class \code{"MethodsFunction"}, see \sQuote{Value} below.}
47
  \item{byclass}{an optional \code{\link{logical}} allowing to override
48
    the \code{"byclass"} attribute determining how the result is
49
    printed, see \sQuote{Details}.}
50
  \item{\dots}{potentially further arguments passed to and from methods;
51
    unused currently.}
27442 ripley 52
}
67997 morgan 53
 
54
\details{
55
  \code{methods()} finds S3 and S4 methods associated with either the
84038 maechler 56
  \code{generic.function} or \code{class} argument.
57
  Methods found are those provided by all loaded namespaces via
58
  registration, see \code{\link{UseMethod}}; normally, this includes
67997 morgan 59
  all packages on the current \code{search()} path.  \code{.S3methods()}
84038 maechler 60
  finds only S3 methods, \code{\link{.S4methods}()} finds only S4 methods.
67997 morgan 61
 
62
  When invoked with the \code{generic.function} argument, the
68162 maechler 63
  \code{"byclass"} attribute (see Details) is \code{FALSE}, and the
64
  \code{print} method by default displays the signatures (full names) of
65
  S3 and S4 methods.  S3 methods are printed by pasting the generic
66
  function and class together, separated by a \sQuote{.}, as
67997 morgan 67
  \code{generic.class}.  The S3 method name is followed by an asterisk
68
  \code{*} if the method definition is not exported from the package
69
  namespace in which the method is defined.  S4 method signatures are
90002 hornik 70
  printed as \samp{generic,class-method}; S4 allows for multiple
67997 morgan 71
  dispatch, so there may be several classes in the signature
90002 hornik 72
  \samp{generic,A,B-method}.
67997 morgan 73
 
68162 maechler 74
  When invoked with the \code{class} argument, \code{"byclass"} is
75
  \code{TRUE}, and the \code{print} method by default displays the names
76
  of the generic functions associated with the class, \code{generic}.
67997 morgan 77
 
78
  The source code for all functions is available.  For S3 functions
79
  exported from the namespace, enter the method at the command line as
80
  \code{generic.class}.  For S3 functions not exported from the
81
  namespace, see \code{getAnywhere} or \code{getS3method}.  For S4
82
  methods, see \code{getMethod}.
83
 
84
  Help is available for each method, in addition to each generic.  For
85
  interactive help, use the documentation shortcut \code{?} with the
86
  name of the generic and tab completion, \code{?"generic<tab>"} to
87
  select the method for which help is desired.
88
 
68087 morgan 89
  The S3 functions listed are those which \emph{are named like methods}
90
  and may not actually be methods (known exceptions are discarded in the
91
  code).
67997 morgan 92
}
93
 
27442 ripley 94
\value{
67997 morgan 95
  An object of class \code{"MethodsFunction"}, a character vector of
96
  method names with \code{"byclass"} and \code{"info"} attributes.  The
68162 maechler 97
  \code{"byclass"} attribute is a \code{\link{logical}} indicating if
98
  the results were obtained with argument \code{class}
99
  defined.  The \code{"info"} attribute is a data frame with columns:
67997 morgan 100
  \describe{
68162 maechler 101
    \item{generic}{\code{\link{character}} vector of the names of the generic.}
84495 maechler 102
    \item{visible}{logical(), is the method \dQuote{visible} to the user?
103
      When true, it typically is exported from the namespace of the package
104
      in which it is defined, and the package is \code{\link{attach}()}ed
105
      to the \code{\link{search}()} path.}
67997 morgan 106
    \item{isS4}{logical(), true when the method is an S4 method.}
68162 maechler 107
    \item{from}{a \code{\link{factor}}, the location or package name
108
      where the method was found.}
67997 morgan 109
  }
27442 ripley 110
}
111
 
112
\note{
85953 hornik 113
  The original \code{methods} function was written by \I{Martin Maechler}.
27442 ripley 114
}
67997 morgan 115
 
27442 ripley 116
\seealso{
49567 ripley 117
  \code{\link{S3Methods}}, \code{\link{class}}, \code{\link{getS3method}}.
44781 maechler 118
 
67997 morgan 119
  For S4, \code{\link{getMethod}}, \code{\link{showMethods}},
71369 jmc 120
  \link[methods]{Introduction} or \code{\link{Methods_Details}}.
27442 ripley 121
}
67997 morgan 122
 
27442 ripley 123
\references{
88585 hornik 124
  \bibshow{R:Chambers:1992:aa}
27442 ripley 125
}
126
\examples{
77517 maechler 127
methods(class = "MethodsFunction") # format and print
128
 
41513 ripley 129
require(stats)
130
 
27442 ripley 131
methods(summary)
67997 morgan 132
methods(class = "aov")    # S3 class
68162 maechler 133
## The same, with more details and more difficult to read:
134
print(methods(class = "aov"), byclass=FALSE)
67997 morgan 135
methods("[[")             # uses C-internal dispatching
35633 ripley 136
methods("$")
67997 morgan 137
methods("$<-")            # replacement function
138
methods("+")              # binary operator
139
methods("Math")           # group generic
41502 ripley 140
require(graphics)
77517 maechler 141
methods(axis)             # looks like a generic, but is not
67997 morgan 142
 
77517 maechler 143
mf <- methods(format)     # quite a few; ... the last few :
144
tail(cbind(meth = format(mf)))
145
 
80296 ripley 146
if(require(Matrix, quietly = TRUE)) {
68005 ripley 147
print(methods(class = "Matrix"))  # S4 class
77517 maechler 148
m <- methods(dim)         # S3 and S4 methods
68005 ripley 149
print(m)
150
print(attr(m, "info"))    # more extensive information
67997 morgan 151
 
44781 maechler 152
## --> help(showMethods) for related examples
153
}
68005 ripley 154
}
27442 ripley 155
\keyword{methods}