Like, I have four class gevp
, gpdp
, and others two, and I created generic function, plot
and summary
for each the four classes. And I were wonder if can I create just one generic function plot
and summary
, for the four classes, like inside that, I gonna choose the object of class that I want to.
If you want to learn more about managing classes and objects, I recommend Hadley's excellent book Advanced R. The relevant chapter is available online here. I don't know your underlying code, and there are differences between the S3 and the fancier S4 systems, so here's the most simple possible way of defining functions specific to your class.
The key is writing a function call in the format function.class
. The print.gevp
function should contain whatever function you've already written for printing objects of class gevp
. Be careful with this, since you could end up creating a function for print
that has nothing to do with printing.
# Generate test data
some_data <- rnorm(3)
class(some_data) <- "gevp"
more_data <- runif(3)
class(more_data) <- "gpdp"
# Create examples of the print functions for both classes
print.gevp <- function(dta) {
print(paste0("Here is a value of class gevp: ", dta))
}
print.gpdp <- function(dta) {
print(paste0("Here is a value of class gpdp: ", dta))
}
# Notice how calling the generic print function now invokes the specific
# function for each class
print(some_data)
print(more_data)