Learn R Programming

modules (version 0.9.8)

register_S3_method: Register an S3 method for a given generic and class.

Description

Register an S3 method for a given generic and class.

Usage

register_S3_method(name, class, method)

Arguments

name

the name of the generic as a character string

class

the class name

method

the method to register

Details

Methods for generics defined in the same module do not need to be registered explicitly, and indeed should not be registered. However, if the user wants to add a method for a known generic (e.g. print), then this needs to be made known explicitly.

Examples

Run this code
# NOT RUN {
# In module a:
print.my_class = function (x) {
    cat(sprintf('My class with field %s\n', x$field))
    invisible(x)
}

register_S3_method('print', 'my_class', print.my_class)

# Globally:
a = import('a')
obj = structure(list(field = 42), class = 'my_class')
obj # calls `print`, with output "My class with field 42"
# }

Run the code above in your browser using DataLab