From: Martin Morgan <mtmorgan_at_fhcrc.org>

Date: Sat, 15 Mar 2008 15:30:32 -0700

*>
*

*> Well you're right. Consider the following code :
*

*>
*

*> setClass("A",representation(nb="numeric",li="list"))
*

*> setMethod(
*

*> "show",
*

*> "A",
*

*> function(object){
*

*> for(i in 1:object_at_nb){print(object_at_li[i])}
*

*> }
*

*> )
*

*>
*

*> Reading what you say (but I did non try yet), I think to define a length function for class "A" that will be the sum of the length of all the slot, then I will test length(a)!=0
*

*>
*

> Christophe

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 15 Mar 2008 - 22:34:48 GMT

Date: Sat, 15 Mar 2008 15:30:32 -0700

I replied off list, accidentally, below. Christophe then asked

>> Probably you want to write a method for 'length' with signature x = 'E'.

You want to use seq_along(object_at_li) rather than 1:object_at_nb (which fails when object_at_nb is 0-length)

> x <- list() > for (i in seq_along(x)) cat(x[[i]], "\n") >

(no error, no output). 'for (i in 1:x)'-style iterations also surprise when x=0. Also, 'cat' is used more often in 'show' methods, perhaps because it imposes less formating on the output and hence allows the 'show' method to have more control over compactly representing the complex S4 objects.

*> # this works
**> new("A",nb=2,li=list("E","R"))
**>
**> # this does not
**> new("A")
**>
*

> So I want to define show with first detecting if the object A is the empty object or not.

*>
*

>> Probably you want to write a method for 'length' with signature x = 'E'. > slotNames and getSlots might help you to write a function that visits all slots.

> Christophe

[off-list reply, oops]

Christophe Genolini wrote:

> Hi the list, > > With basic type, length gives the length of the vector > Wtih list, length gives the number of item > With S4 object, length gives...one. Even with an objet with empty slot.

not quite; if the class extends a class for which there is a length method, then, e.g.,

> setClass("E", "numeric")

[1] "E"

> length(new("E"))

[1] 0

Probably you want to write a method for 'length' with signature x = 'E'.

slotNames and getSlots might help you to write a function that visits all slots. object.size will give you the physical size of the object, but this is probably not what you want.

Martin

> setClass("E",representation(e="numeric")) > [1] "E" > length(new("E")) > [1] 1 > setClass("E",representation(e="matrix")) > [1] "E" > length(new("E")) > [1] 1 > > Is there a way to get the real length of an S4 objet ? Furthermore,is there a simple way to detect that an object is has all its slot to object of size zero ?

> > Thanks > > Christophe ______________________________________________R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 15 Mar 2008 - 22:34:48 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Sat 15 Mar 2008 - 23:30:21 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*