我很震惊地发现该节目是S4通用的,而且我找不到使用S3调度来使show函数工作的方法.一个简单的演示:
> 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
Run Code Online (Sandbox Code Playgroud)
没有S3在这里调度......
> 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
Run Code Online (Sandbox Code Playgroud)
你觉得呢?
> 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
Run Code Online (Sandbox Code Playgroud)
对于打印它是有效的.
我有很好的理由继续使用S3(其中很大一部分是开销的最小化,因为对象将在bootstrapping中广泛使用).我该如何在这里定义不同的节目和打印方法?
或许
setOldClass("aClass")
setMethod(show, "aClass", function(object) cat("S4\n"))
print.aClass <- function(object) { cat("S3... "); show(object) }
Run Code Online (Sandbox Code Playgroud)
进而
> structure(1:5, class="aClass")
S3... S4
Run Code Online (Sandbox Code Playgroud)
但我不太明白你想做什么。
| 归档时间: |
|
| 查看次数: |
1005 次 |
| 最近记录: |