Learn R Programming

R.methodsS3 (version 1.2.1)

getMethodS3: Get an S3 method

Description

Get an S3 method.

Usage

## S3 method for class 'default':
getMethodS3(name, class="default", envir=parent.frame(), ...)

Arguments

name
The name of the method.
class
The class of the method.
envir
The environment from which the search for the S3 method is done.
...
Not used.

See Also

This is just a conveniency wrapper around getS3method that have arguments consistent with setMethodS3(). getGenericS3().

Examples

Run this code
######################################################################
# Example 1
######################################################################
setMethodS3("foo", "default", function(x, ...) {
  cat("In default foo():
");
  print(x, ...);
})


setMethodS3("foo", "character", function(s, ...) {
  cat("In foo() for class 'character':
");
  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.
");
}


# Your defintion; will redefine bar() above to bar.default().
setMethodS3("bar", "character", function(object, ...) {
  cat("In bar() for class 'character':
");
  print(object, ...);
})

bar(123)
bar("123")

Run the code above in your browser using DataLab