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.

- 5
- 3
-
I don't know the specifics of your code, but in some cases you could use a control function (like a `switch`) to test what kind of class is being used and then direct to the appropriate code. – Andrew Brēza Jul 08 '16 at 15:20
-
Can you give me a example using `switch`? because this structure is not really familiar to me. @AndrewBrēza – aliocha Jul 13 '16 at 14:07
-
I decided to put more work into it and make it work the right way. I can still provide an example with `switch` if you'd like. – Andrew Brēza Jul 15 '16 at 14:15
-
I gonna appreciate if you provide me a example using `switch`@AndrewBrēza – aliocha Jul 15 '16 at 14:37
2 Answers
You can use inheritance to your advantage here, and assign a vector of values to the class of an object. when print
doesn't find a method for class_x
, it will proceed to look for a method of class generic_class
.
x <- structure("This is the object of class 'class-x'",
class = c("class_x", "generic_class"))
y <- structure("This is the object of class 'class-y'",
class = c("class_y", "generic_class"))
print.generic_class <- function(x, ...)
{
print("This prints the same thing for 'class_x' and 'class_y'")
}
x
y

- 16,897
- 6
- 45
- 65
-
great suggestion, I tried and worked. Thanks! Do you know how to use the suggestion given by @AndrewBrēza using `switch` ? @Benjamin – aliocha Jul 13 '16 at 23:09
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)

- 7,705
- 3
- 34
- 40
-
thanks for the recommendation, I understood clearly your suggestion, I will read it. In my code my methods are different for each class, like for the `gpdp` has a difference between the `gevp` inside. So, I think a have to put some condition, if the class is `gevp` thus it will do a especific part of the code, and if is `gpdp` thus will do another part. Do you know how to do this correctly? @AndrewBrēza – aliocha Jul 15 '16 at 14:36
-
my question initially was how to create just one `print` (for example) generic function and inside that method, when I called `print` the method will do the part of which class are appropriate, in others words I will create just one generec function for all class that I have @AndrewBrēza – aliocha Jul 15 '16 at 15:20