Вопрос

I'm quite stunned to find out that show is an S4 generic, and that I can't find a way to use the S3 dispatching to get a show function to work. A simple demonstration:

> 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 S3 dispatching 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 did 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 (of which a big part is the minimization of overhead, as the objects will be used extensively in bootstrapping). How am I supposed to define a different show and print method here?

Это было полезно?

Решение

Maybe

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'm not really understanding what you want to do.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top