To get started, I will give an example with S3classes:
setClass("S3_class", representation("list"))
S3_obj <- new("S3_class", list(x=sample(10), y=sample(10)))
Now you can overload internal functions, for example length, in your class (you can also operator-overload):
length.S3_class <- function(x) sapply(x, length)
length(S3_obj)
Or, alternatively, you can have your own function with any name, where you can check if the object is a class S3_classand do something:
len <- function(x) {
if (class(x) != "S3_class") {
stop("object not of class S3_class")
}
sapply(x, length)
}
> len(S3_obj)
> len(1:10)
S4 (, S3), , ( , ). , ( S3, , ).