问题
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