setMethodS3 | R Documentation |
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"), ...)
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.