[Rd] Destructive str(...)?

Simon Urbanek simon.urbanek at math.uni-augsburg.de
Sat Oct 30 05:28:54 CEST 2004


I have encountered a strange behavior of the str function - it seems to 
modify the object that is displayed. Probably I'm using something 
unsupported (objects consisting just of an external reference), but 
still I'm curious as of why this happens. I create (in C code) 
EXTPTRSXP and associate a class to it via SET_CLASS. Such objects works 
fine until it's passed to str as the following output demonstrates:

 > c<-.MCall("RController","getRController")
 > c
[1] "<RController: 0x3be5d0>"
 > str(c)
Class 'ObjCid' length 1 <pointer: 0x3be5d0>
 > c
<pointer: 0x3be5d0>
 > str(c)
length 1 <pointer: 0x3be5d0>

The .MCall basically produces an external reference and assigns a class 
(ObjCid) to it. There's a corresponding print method and it works fine. 
However, when str is called, it strips the class information from the 
object as a repeated call to str also shows:

  > str(c); str(c)
Class 'ObjCid' length 1 <pointer: 0x3be5d0>
length 1 <pointer: 0x3be5d0>

Is this behavior intentional, undocumented or simply wrong?

Cheers,
Simon

[Tested with R 2.0.0 release (2004-10-04) on Mac OS X 10.3.5 - I have 
currently no other machine to test it on, but I very much suspect that 
this is platform-independent.]

the C code used to generate the object:

SEXP class, sref = R_MakeExternalPtr((void*) obj, R_NilValue, 
R_NilValue);
PROTECT(class = allocVector(STRSXP, 1));
SET_STRING_ELT(class, 0, mkChar("ObjCid"));
SET_CLASS(sref, class);
UNPROTECT(1);



More information about the R-devel mailing list