|
|||||
| Centre for Mathematical Sciences, Lund University |
|
||||
|
Home Up to Help Docs R Help Packages CRAN R.oo aroma Install The R software Most packages Tips'n'Tricks install.packages Rgui (Windows) External Links R homepage Download R bioconductor.org braju.com packages Printer-friendly Print this page |
Going from S3 to S4 object oriented programming in [R]January 2002, Henrik Bengtsson Please note that these notes was written in early 2002 when I was not that familiar with the methods package, which also has evolved quite a bit since then. Therefore, some of the comments below might be incorrect or invalid.IntroductionStarting with [R] v1.4.0 a new class formalism referred to as S4 classes/objects is introduced which is implemented in the package methods by John Chambers [1,2]. Methods in S3
Assume that we have an object refered to as
getArea.Rectangle <- function(object) {
attr(object, "sideA") * attr(object, "sideB");
}
given that a
getArea <- function(object, ...) UseMethod("getArea");
When this function is called by Methods in S4Below examples will show how you can move from using S3 style of calling class methods to the S4 style. Define a generic functionIn some situations the generic function are generated automatically, but if you would like to define a generic function explicitly it can be done by
setGeneric("getArea", function(object, ...) standardGeneric("getArea"));
Note how Define a method specific to a class
A method for a specific class is defined using the
getArea.Rectangle <- function(object, ...) {
object@sideA * object@sideB;
}
setMethod("getArea", "Rectangle", getArea.Rectangle);
Alternatively it is possible to directly define it as
setMethod("getArea", "Rectangle", function(object, ...) {
stop(paste("Method getArea() is not defined for this class:",
data.class(object)));
})
It is important to know that the methods must have exactly the same arguments as the generic function. Since we defined the generic function to have the arguments Define a default methodPostponing the explanation, a default method can be defined, using S4 style, as
setMethod("getArea", "ANY", function(object, ...) {
stop(paste("Method getArea() is not defined for this class:",
data.class(object)));
})
The signature
Error in setMethod("getArea", "ANY", function(object, ...) { :
No existing definition for function "getArea"
Define a no-argument function with the same name as a generic function
Here the signature "
getArea.missing <- function() {
cat("Method getArea() was called without arguments.\n");
}
setMethod("getArea", "missing", function(object, ...) getArea.missing());
You might wonder why we do not define the method as Define a method with name that is already taken
It sometimes happens that one needs to define a method of a class, but there is already a function with the same name and it does not match your arguments. Take for instance the case where one wants to define the method
if (!exists("licence.missing")) licence.missing <- licence;
setGeneric("licence", function(object, ...) standardGeneric("licence"));
setMethod("licence", "missing", function(object, ...) licence.missing());
Note how the
setMethod("licence", "Software", function(object, ...) {
# Some code here.
});
References
|
|
Email: hb@maths.lth.se
|