File: setMethodS3.Rd

package info (click to toggle)
r-cran-r.methodss3 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 300 kB
  • sloc: sh: 12; makefile: 2
file content (131 lines) | stat: -rw-r--r-- 5,293 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Do not modify this file since it was automatically generated from:
% 
%  030.setMethodS3.R
% 
% by the Rdoc compiler part of the R.oo package.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\name{setMethodS3}
\alias{setMethodS3.default}
\alias{setMethodS3}

\title{Creates an S3 method}

\description{
 Creates an S3 method. A function with name \code{<name>.<class>} will
 be set to \code{definition}. The method will get the modifiers specified
 by \code{modifiers}.  If there exists no generic function for this method,
 it will be created automatically.
}

\usage{
\method{setMethodS3}{default}(name, class="default", definition, private=FALSE, protected=FALSE,
  export=FALSE, static=FALSE, abstract=FALSE, trial=FALSE, deprecated=FALSE,
  envir=parent.frame(), overwrite=TRUE, conflict=c("warning", "error", "quiet"),
  createGeneric=TRUE, exportGeneric=TRUE, appendVarArgs=TRUE,
  validators=getOption("R.methodsS3:validators:setMethodS3"), ...)
}

\arguments{
  \item{name}{The name of the method.}
  \item{class}{The class for which the method should be defined. If
     \code{class == "default"} a function with name \code{<name>.default}
     will be created.}
  \item{definition}{The method definition.}
  \item{private, protected}{If \code{private=TRUE}, the method is declared
     private. If \code{protected=TRUE}, the method is declared protected.
     In all other cases the method is declared public.}
  \item{export}{A \code{\link[base]{logical}} setting attribute \code{"export"}.}
  \item{static}{If \code{\link[base:logical]{TRUE}} this method is defined to be static,
     otherwise not. Currently this has no effect expect as an indicator.}
  \item{abstract}{If \code{\link[base:logical]{TRUE}} this method is defined to be abstract,
     otherwise not. Currently this has no effect expect as an indicator.}
  \item{trial}{If \code{\link[base:logical]{TRUE}} this method is defined to be a trial method,
     otherwise not. A trial method is a method that is introduced to be
     tried out and it might be modified, replaced or even removed in a
     future release. Some people prefer to call trial versions, beta
     version. Currently this has no effect expect as an indicator.}
  \item{deprecated}{If \code{\link[base:logical]{TRUE}} this method is defined to be deprecated,
     otherwise not. Currently this has no effect expect as an indicator.}
  \item{envir}{The environment for where this method should be stored.}
  \item{overwrite}{If \code{\link[base:logical]{TRUE}} an already existing generic function and an
     already existing method with the same name (and of the same class)
     will be overwritten, otherwise not.}
  \item{conflict}{If a method already exists with the same name (and of
     the same class), different actions can be taken. If \code{"error"},
     an exception will be thrown and the method will not be created.
     If \code{"warning"}, a \code{\link[base]{warning}} will be given and the method \emph{will}
     be created, otherwise the conflict will be passed unnoticed.}
  \item{createGeneric, exportGeneric}{If \code{createGeneric=TRUE},
     a generic S3/UseMethod function is defined for this method,
     iff missing, and \code{exportGeneric} species attribute
     \code{"export"} of it.}
  \item{appendVarArgs}{If \code{\link[base:logical]{TRUE}}, argument \code{...} is added with a
     warning, if missing.  For special methods such as \code{$} and
     \code{[[}, this is never done (argument is ignored).
     This will increase the chances that the method is consistent with a
     generic function with many arguments and/or argument \code{...}.}
  \item{validators}{An optional \code{\link[base]{list}} of \code{\link[base]{function}}s that can be used
     to assert that the generated method meets certain criteria.}
  \item{...}{Passed to \code{\link{setGenericS3}}(), iff called.}
}

\examples{
######################################################################
# Example 1
######################################################################
setMethodS3("foo", "default", function(x, ...) {
  cat("In default foo():\n");
  print(x, ...);
})


setMethodS3("foo", "character", function(s, ...) {
  cat("In foo() for class 'character':\n");
  print(s, ...);
})

# The generic function is automatically created!
print(foo)

foo(123)
foo("123")


######################################################################
# Example 2
#
# Assume that in a loaded package there is already a function bar(),
# but you also want to use the name 'bar' for the character string.
# It may even be the case that you do not know of the other package,
# but your users do!
######################################################################
# bar() in other package
bar <- function(x, y, ...) {
  cat("In bar() of 'other' package.\n");
}


# Your definition; will redefine bar() above to bar.default().
setMethodS3("bar", "character", function(object, ...) {
  cat("In bar() for class 'character':\n");
  print(object, ...);
})

bar(123)
bar("123")


}

\seealso{
  For more information about S3, see \code{\link[base]{UseMethod}}().
}

\author{Henrik Bengtsson}



\keyword{programming}
\keyword{methods}