# NOT RUN {
######################################################################
# 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")
# }
Run the code above in your browser using DataLab