Title: | S3 Methods Simplified |
---|---|
Description: | Methods that simplify the setup of S3 generic functions and S3 methods. Major effort has been made in making definition of methods as simple as possible with a minimum of maintenance for package developers. For example, generic functions are created automatically, if missing, and naming conflict are automatically solved, if possible. The method setMethodS3() is a good start for those who in the future may want to migrate to S4. This is a cross-platform package implemented in pure R that generates standard S3 methods. |
Authors: | Henrik Bengtsson [aut, cre, cph] |
Maintainer: | Henrik Bengtsson <[email protected]> |
License: | LGPL (>= 2.1) |
Version: | 1.8.2 |
Built: | 2024-11-15 04:51:17 UTC |
Source: | https://github.com/HenrikBengtsson/R.methodss3 |
Methods that simplify the setup of S3 generic functions and S3 methods. Major effort has been made in making definition of methods as simple as possible with a minimum of maintenance for package developers. For example, generic functions are created automatically, if missing, and naming conflict are automatically solved, if possible. The method setMethodS3() is a good start for those who in the future may want to migrate to S4. This is a cross-platform package implemented in pure R that generates standard S3 methods. This contents of this package originates from the R.oo package [1].
To install this package do
install.packages("R.methodsS3")
To get the "devel" version, see https://github.com/HenrikBengtsson/R.methodsS3/.
This package only requires a standard R installation.
To get started, see:
setMethodS3
() - Simple and safe creation of S3 methods
and, whenever needed, automatic creation of S3 generic function.
For a detailed introduction to the package, see [1].
Whenever using this package, please cite [1] as
Bengtsson, H. The R.oo package - Object-Oriented Programming with References Using Standard R Code, Proceedings of the 3rd International Workshop on Distributed Statistical Computing (DSC 2003), ISSN 1609-395X, Hornik, K.; Leisch, F. & Zeileis, A. (ed.), 2003
The releases of this package is licensed under LGPL version 2.1 or newer.
Henrik Bengtsson
[1] H. Bengtsson, The R.oo package - Object-Oriented Programming with References Using Standard R Code, In Kurt Hornik, Friedrich Leisch and Achim Zeileis, editors, Proceedings of the 3rd International Workshop on Distributed Statistical Computing (DSC 2003), March 20-22, Vienna, Austria. https://www.r-project.org/conferences/DSC-2003/Proceedings/
Gets an S3 generic function.
## Default S3 method: getGenericS3(name, envir=parent.frame(), inherits=TRUE, ...)
## Default S3 method: getGenericS3(name, envir=parent.frame(), inherits=TRUE, ...)
name |
The name of the generic function. |
envir |
The |
inherits |
A |
... |
Not used. |
Henrik Bengtsson
setGenericS3
().
getMethodS3
().
isGenericS3
().
Gets an S3 method.
## Default S3 method: getMethodS3(name, class="default", envir=parent.frame(), ...)
## Default S3 method: getMethodS3(name, class="default", envir=parent.frame(), ...)
name |
The name of the method. |
class |
The class of the method. |
envir |
The |
... |
Not used. |
Henrik Bengtsson
This is just a conveniency wrapper around getS3method
that have arguments consistent with setMethodS3
().
getGenericS3
().
Checks if a function is a S3 generic function.
## Default S3 method: isGenericS3(fcn, envir=parent.frame(), ...)
## Default S3 method: isGenericS3(fcn, envir=parent.frame(), ...)
fcn |
|
envir |
If argument |
... |
Not used. |
A function is considered to be a generic S3/UseMethod function if
its name matches one of the known S3 generic functions, or if it
calls UseMethod()
.
Returns TRUE
if a generic S3/UseMethod function, otherwise FALSE
.
Henrik Bengtsson
Note that this method is a internal method called by
setMethodS3
() and there is no reason for calling it directly!
Creates a generic function in S3 style, i.e. setting a
function with name name
that dispatches the method name
via UseMethod
. If there is already a function named name
that function is renamed to name.default
.
## Default S3 method: setGenericS3(name, export=TRUE, envir=parent.frame(), dontWarn=getOption("dontWarnPkgs"), validators=getOption("R.methodsS3:validators:setGenericS3"), overwrite=FALSE, ...)
## Default S3 method: setGenericS3(name, export=TRUE, envir=parent.frame(), dontWarn=getOption("dontWarnPkgs"), validators=getOption("R.methodsS3:validators:setGenericS3"), overwrite=FALSE, ...)
name |
The name of the generic function. |
export |
A |
envir |
The environment for where this method should be stored. |
dontWarn |
If a non-generic method with the same name is found it
will be "renamed" to a default method. If that method is found in
a package with a name that is not found in |
validators |
An optional |
... |
Not used. |
overwrite |
If |
Henrik Bengtsson
To define a method for a class see setMethodS3
().
For more information about S3, see UseMethod
().
myCat.matrix <- function(..., sep=", ") { cat("A matrix:\n") cat(..., sep=sep) cat("\n") } myCat.default <- function(..., sep=", ") { cat(..., sep=sep) cat("\n") } setGenericS3("myCat") myCat(1:10) mat <- matrix(1:10, ncol=5) myCat(mat)
myCat.matrix <- function(..., sep=", ") { cat("A matrix:\n") cat(..., sep=sep) cat("\n") } myCat.default <- function(..., sep=", ") { cat(..., sep=sep) cat("\n") } setGenericS3("myCat") myCat(1:10) mat <- matrix(1:10, ncol=5) myCat(mat)
Creates an S3 method. A function with name <name>.<class>
will
be set to definition
. The method will get the modifiers specified
by modifiers
. If there exists no generic function for this method,
it will be created automatically.
## Default S3 method: setMethodS3(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"), ...)
## Default S3 method: setMethodS3(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"), ...)
name |
The name of the method. |
class |
The class for which the method should be defined. If
|
definition |
The method definition. |
private , protected
|
If |
export |
A |
static |
If |
abstract |
If |
trial |
If |
deprecated |
If |
envir |
The environment for where this method should be stored. |
overwrite |
If |
conflict |
If a method already exists with the same name (and of
the same class), different actions can be taken. If |
createGeneric , exportGeneric
|
If |
appendVarArgs |
If |
validators |
An optional |
... |
Passed to |
Henrik Bengtsson
For more information about S3, see UseMethod
().
###################################################################### # 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")
###################################################################### # 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")