Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'show' a normal S4 generic function?

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?

like image 234
Eli Sander Avatar asked Jul 11 '12 18:07

Eli Sander


People also ask

What is S4 function?

S4 provides a formal approach to functional OOP. The underlying ideas are similar to S3 (the topic of Chapter 13), but implementation is much stricter and makes use of specialised functions for creating classes ( setClass() ), generics ( setGeneric() ), and methods ( setMethod() ).

What is a S4 function in R?

The S4 system in R is a system for object oriented programing. Confusingly, R has support for at least 3 different systems for object oriented programming: S3, S4 and S5 (also known as reference classes).

What is generic name function?

In some systems for object-oriented programming such as the Common Lisp Object System (CLOS) and Dylan, a generic function is an entity made up of all methods having the same name. Typically a generic function is an instance of a class that inherits both from function and standard-object.

What is a generic function in R?

A generic function is one which may be applied to different types of inputs producing results depending on the type of input. Examples are plot() and summary() We demonstrate these differences using sample data from. Dalgaard's Introductory Statistics with R.


1 Answers

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.

like image 153
BenBarnes Avatar answered Sep 30 '22 18:09

BenBarnes