[Rd] S4 methods and polymorophism
Eric Lecoutre
lecoutre at stat.ucl.ac.be
Wed Sep 22 10:21:48 CEST 2004
Hello R Power Users,
There is a rather large introduction you may skip to go directly to
my question about S4 classes.
I am working on some toy code to help me get in through S4 classes.
Doing this homework, I have some questions about S4 classes. I have
carefully read available help and some additional material such as Robert
Gentleman's slides and special attention to "Programming with Data".
I am working with R 1.9 on Windows.
Final goal is the complete rewriting of my R2HTML package from scratch to
provide to users a convenient way to describe their output and to allow
different output format (provided by special functions - drivers, "à la"
Sweave - HTML, LaTeX, XML...).
As BillGates guys often have good ideas (or find or buy them), I take the
structure of Word documents as a starting point.
A document will be a collection of Objects, which will be either a standard
R object or a graph, in both cases with some Formatting Options (FO) added.
Formatting Options (a class) is a list of named components like (font-size:
16, forecolor: darkred).
Here is what I have for the moment:
> setClassUnion("ROMA_ID", c("NULL","character"))
> setClass("Objects",representation("list",ID="ROMA_ID"))
ie: Objects is a list (there will be objects of class "Object" without "s")
and ID provided to all objects.
Then, as I will manipulate several such collections, such as Documents,
Graphs, and so on, I define the virtual class Collection. At the moment:
> setClassUnion("Collection",c("Objects","Documents","Graphs"))
Then, I follow Chambers's samples to add some functionality such as show()
functions and manipulations. A "+" method applied on two "Object" creates a
new Objects collection.
As you can see from this output, both the definition of the mehod for "+"
and for "show" are correct:
---------- output ---------------
> Object(1)+Object("Oui")
<ROMA> Collection of Objects: 2 objects.
ID: numeric
<ROMA> Object
Classe: numeric
FO: alignment
ID: character
<ROMA> Object
Classe: character
FO: font;fontsize;fontcolor;alignment
---------- output ---------------
I have succedded adding a method to generic "+" operator, which is
initially defined as:
> getMethod("+")
.Primitive("+")
As I understand it, the fact that it is a .Primitive call ensures the
default method for any extendion (that is: we can add methods).
Now, I try to add a method for length to handle "Collection" classes.
> getMethod("length")
.Primitive("length")
*** QUESTION:
I dont undertand the following error message when trying to define this method:
> setMethod("length","Collection",function(object){ length(object at ID)})
Error in conformMethod(signature, mnames, fnames, f) :
In method for function "length": Formal arguments omitted in the
method definition cannot be in the signature (x = "Collection")
How is it possible to extend length to my own class?
Be HappyR and HappyR again,
Eric
Eric Lecoutre
UCL / Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium
tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
If the statistics are boring, then you've got the wrong numbers. -Edward
Tufte
More information about the R-devel
mailing list