RE: [Rd] RE: [R] Testing for S4 objects

From: Prof Brian Ripley <>
Date: Sun 12 Dec 2004 - 05:56:59 EST

On Sat, 11 Dec 2004, John Fox wrote:

> Thanks for this. I'll give your new definition of isS4object() a try. The
> function is used in my Rcmdr package, which provides a basic-statistics GUI
> for R. When an object -- the result of executing a command -- is printed, I
> test whether it is an S4 object to decide whether to use show() or print().

I think that *if* you have methods attached, it is OK there to always call show(). (You will not be using extra args to print(), e.g. digits, I presume.) show() will call S3 methods for print() if it is given a non-S4 classed object.

(Sorry, if I had realised what this about, I should have said that earlier.)

As I understand it (and I wrote some of it)

     if(tryS4 && isObject(x) && isMethodsDispatchOn()) {
 	SEXP class = getAttrib(x, R_ClassSymbol);
 	if(length(class) == 1) {
 	    /* internal version of isClass() */
 	    char str[201];
 	    snprintf(str, 200, ".__C__%s", CHAR(STRING_ELT(class, 0)));
 	    if(findVar(install(str), rho) != R_UnboundValue)
 		callShow = TRUE;

where tryS4 is to ensure this is tried only once. So an object is an S4 object on which show() is to be called if it has a properly registered (S3 or S4) class (the object bit, tested by isObject) of length one, and the class is registered by the methods code.


Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Sun Dec 12 06:04:24 2004

This archive was generated by hypermail 2.1.8 : Sun 12 Dec 2004 - 06:32:29 EST