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.
The class for which the method should be defined. If
class == "default" a function with name <name>.default
will be created.
definition
The method defintion.
private, protected
If private=TRUE, the method is declared
private. If protected=TRUE, the method is declared protected.
In all other cases the method is declared public.
export
A logical setting attribute "export".
static
If TRUE this method is defined to be static,
otherwise not. Currently this has no effect expect as an indicator.
abstract
If TRUE this method is defined to be abstract,
otherwise not. Currently this has no effect expect as an indicator.
trial
If 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.
deprecated
If TRUE this method is defined to be deprecated,
otherwise not. Currently this has no effect expect as an indicator.
envir
The environment for where this method should be stored.
overwrite
If TRUE an already existing method with the same
name (and of the same class) will be overwritten, otherwise not.
conflict
If a method already exists with the same name (and of
the same class), different actions can be taken. If "error",
an exception will be thrown and the method will not be created.
If "warning", a warning will be given and the method will
be created, otherwise the conflict will be passed unnotice.
createGeneric, exportGeneric
If createGeneric=TRUE,
a generic S3/UseMethod function is defined for this method,
iff missing, and exportGeneric species attribute
"export" of it.
appendVarArgs
If TRUE, argument ... is added with a
warning, if missing. For special methods such as $ and
[[, 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 ....
validators
An optional list of functions that can be used
to assert that the generated method meets certain criteria.
...
Passed to setGenericS3(), iff called.
Author(s)
Henrik Bengtsson
See Also
For more information about S3, see UseMethod().
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 defintion; 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")
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(R.methodsS3)
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/R.methodsS3/setMethodS3.Rd_%03d_medium.png", width=480, height=480)
> ### Name: setMethodS3
> ### Title: Creates an S3 method
> ### Aliases: setMethodS3.default setMethodS3
> ### Keywords: programming methods
>
> ### ** 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, ...);
+ })
NULL
>
> # The generic function is automatically created!
> print(foo)
function (...)
UseMethod("foo")
attr(,"export")
[1] TRUE
>
> foo(123)
In default foo():
[1] 123
> foo("123")
In foo() for class 'character':
[1] "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 defintion; will redefine bar() above to bar.default().
> setMethodS3("bar", "character", function(object, ...) {
+ cat("In bar() for class 'character':\n");
+ print(object, ...);
+ })
Warning message:
In setGenericS3.default(name, export = exportGeneric, envir = envir, :
Renamed the preexisting function bar to bar.default, which was defined in environment <R_GlobalEnv>.
>
> bar(123)
In bar() of 'other' package.
> bar("123")
In bar() for class 'character':
[1] "123"
>
>
>
>
>
>
>
> dev.off()
null device
1
>