Define show method for class S3

I am very stunned to learn that the show is a common S4, and that I cannot find a way to use the S3 manager to make the show function work. Simple demo:

> x <- 1:5 > xx <- structure(x,class="aClass") > show.aClass <- function(object){ + cat("S3 dispatching.\n") + print(object) + } > xx [1] 1 2 3 4 5 

No sending S3 here ...

 > setMethod("show","aClass",function(object){ + cat("S4 dispatching.\n") + print(object) + }) in method for 'show' with signature '"aClass"': no definition for class "aClass" [1] "show" > xx [1] 1 2 3 4 5 

What do you think?

 > print.aClass <- function(object){ + cat("the print way...\n") + print(as.vector(object)) #drop class to avoid infinite loop! + } > xx the print way... [1] 1 2 3 4 5 

And for print it works.

I have pretty good reasons to stay with S3 (most of which is to minimize overhead, since the objects will be widely used at boot). How can I define another show and print method here?

+4
source share
1 answer

Can

 setOldClass("aClass") setMethod(show, "aClass", function(object) cat("S4\n")) print.aClass <- function(object) { cat("S3... "); show(object) } 

and then

 > structure(1:5, class="aClass") S3... S4 

But I do not quite understand what you want to do.

+3
source

Source: https://habr.com/ru/post/1385086/


All Articles