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") <environment: 0x55ccff6ee5c8> 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 definition; will redefine bar() above to bar.default(). setMethodS3("bar", "character", function(object, ...) { cat("In bar() for class 'character':\n"); print(object, ...); })
Warning in setGenericS3.default(name, export = exportGeneric, envir = envir, : Renamed the preexisting function bar to bar.default, which was defined in environment base.
bar(123)
In bar() of 'other' package.
bar("123")
In bar() for class 'character': [1] "123"