Skip to content
Prev 139579 / 398506 Next

length of S4

I replied off list, accidentally, below. Christophe then asked
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.
[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