'show'是正常的S4泛型函数吗?

Liz*_*der 4 r show generic-function s4

我正在尝试为我的类创建一个方法,该方法继承自data.frame.我本来希望只是从data.frame继承'show'方法,但我也可以编写自己的方法.我将我的类和'show'方法定义如下:

setClass("SCvec", representation(auth = "character",
    dev = "character",
    sensor = "character",
    channel = "character",
    starttime = "character",
    endtime = "character"),
    contains="data.frame")
setMethod("show", signature(x="SCvec"), function(x) print(x))
Run Code Online (Sandbox Code Playgroud)

当我输入showR控制台时,它会输出:

从包"方法"定义的"show"的standardGeneric

function (object) 
standardGeneric("show")
<bytecode: 0x0396bee8>
<environment: 0x0393ab60>
Methods may be defined for arguments: object
Use  showMethods("show")  for currently available ones.
(This generic function excludes non-simple inheritance; see ?setIs)
Run Code Online (Sandbox Code Playgroud)

所以看起来我不需要自己使用StandardGeneric()将它变成泛型.但是当我运行我的setMethod("show", signature(x="SCvec"), function(x) print(x))线路时,我得到了错误

Error in match.call(definition, call, expand.dots) : 
  unused argument(s) (x = c("SCvec", ""))
Run Code Online (Sandbox Code Playgroud)

我已经定义了这个方法,就像我定义任何其他方法一样.为什么这个方法定义不起作用?'show'与其他通用函数不同吗?

Ben*_*nes 7

该函数show接受一个参数object,因此您需要根据该形式参数定义您的签名和函数定义:

setMethod("show", signature(object="SCvec"), function(object) print(object))
Run Code Online (Sandbox Code Playgroud)

您还可以show通过键入来查看定义的其他方法

showMethods(show)
Run Code Online (Sandbox Code Playgroud)

这表明所有其他方法也是根据类来定义的object.