[Rd] str and class

Martin Maechler maechler at stat.math.ethz.ch
Fri May 9 22:37:21 CEST 2008


>>>>> "HT" == Heather Turner <Heather.Turner at warwick.ac.uk>
>>>>>     on Fri, 09 May 2008 18:00:37 +0100 writes:

    HT> In previous versions of the gnm package, the terms
    HT> component of "gnm" objects had a "classID"
    HT> attribute. This caused problems when used with str as
    HT> the following simple example illustrates:

    >> x <- 1
    >> attr(x, "classID") <- "type1"
    >> str(x)
    HT> Class 'type1' Class 'type1' Class 'type1' Class 'type1' Class 'type1'
    HT> ...
    HT> Error: evaluation nested too deeply: infinite recursion / 
    HT> options(expressions=)?

    HT> The problem is that for S3 objects, str saves attr(object, "class") then 
    HT> recalls str on unclass(object) -- any "class" attribute is removed, but 
    HT> then the "classID" attribute is recursively picked up by attr(object, 
    HT> "class") due to partial matching.

    HT> Obviously the solution is to use more sensibly named attributes, but str 
    HT> could be made more foolproof by using

    HT> attr(object, "class", exact = TRUE)

    HT> instead.

Indeed, thank you, Heather!

Of course, the latter has only been possible in somewhat recent
versions of R, and an alternative would seem to be
   class(object)
anyway.... but that would be wrong, see e.g., class(pi);
however oldClass(object) is really equivalent to the "class"
attribute (with exact match).
I am sure that when I first wrote str(), in 1990, probably for
S-plus version 3.0 *and* for S (as in "S from the Bell Labs"), that
attr(object, "class") and  class(object) hadn't been entirely
equivalent; and in any case, what then was class() is now
oldClass().

I'm committing a bug fix to R-devel and R-patched.

Best regards,
Martin

    HT> Best regards,

    HT> Heather

    HT> -- 
    HT> Dr H Turner
    HT> Research Fellow
    HT> Dept. of Statistics
    HT> The University of Warwick
    HT> Coventry
    HT> CV4 7AL

    HT> Tel: 024 76575870
    HT> Fax: 024 76524532
    HT> Url: www.warwick.ac.uk/go/heatherturner



More information about the R-devel mailing list