str and class

View: New views
2 Messages — Rating Filter:   Alert me  

str and class

by Heather Turner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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

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

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

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

instead.

Best regards,

Heather

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

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

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Re: str and class

by Martin Maechler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>>>>> "HT" == Heather Turner <Heather.Turner@...>
>>>>>     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

______________________________________________
R-devel@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel