Is 'show' a normal S4 generic function?

允我心安 提交于 2019-12-22 08:29:46

问题


I'm trying to create a method for my class, which inherits from data.frame. I was originally hoping just to inherit the 'show' method from data.frame as well, but I'm also fine with writing my own. I defined my class and 'show' method as follows:

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))

when I type show in the R console, it prints out:

standardGeneric for "show" defined from package "methods"

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)

So it sure looks like I don't need to turn it into a generic using StandardGeneric() myself. but when I run my setMethod("show", signature(x="SCvec"), function(x) print(x)) line, I get the error

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

I've defined this method just like I would define any other one. Why does this method definition not work? Is 'show' different than other generic functions?


回答1:


The function show takes an argument object, so you would need to define your signature and function definition with respect to that formal argument:

setMethod("show", signature(object="SCvec"), function(object) print(object))

You can also see other methods that are defined for show by typing in

showMethods(show)

And this shows you that all the other methods are defined in terms of the class of object as well.



来源:https://stackoverflow.com/questions/11439357/is-show-a-normal-s4-generic-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!