[Rd] Strange R object

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 9 05:05:23 CEST 2010


I have *** attached *** an RData file containing an R object that
is acting strangely.

Try this in a fresh workspace. Do not load zoo or any other package.
We load the object, zz2, from the attached RData file.  It is just
the number 1 with the class c("zooreg", "zoo").

Now create an S3 print routine that simply prints an X when given
an object of class "zoo".

If we use print on the object it produces an X but not if we just
enter it at the console.  Also the object is not identical to its
dput output.

How can such an object exist?  What is it about the object that is
different from structure(1, class = c("zoo", "zooreg")) ?

> # load problem object, zz2
> load("problem.RData")
>
> # examine it. Its very simple.
> dput(zz2)
structure(1, class = c("zooreg", "zoo"))
>
> # define a print routine to intercept it
> print.zoo <- function(x, ...) print("X")
>
> # the print routine works as expected
> print(zz2)
[1] "X"
>
> # but not if we use implicit printing!
> zz2
[1] 1
attr(,"class")
>
>
> # I would have expected that the last statement produce an "X" too!
> ###################################################################
>
> # these have the same dput output but are not identical!
> # I would have expected them to be the identical.
> ###################################################################
> aa2 <- structure(1, class = c("zooreg", "zoo"))
> dput(aa2)
structure(1, class = c("zooreg", "zoo"))
> dput(zz2)
structure(1, class = c("zooreg", "zoo"))
> identical(aa2, zz2)
[1] FALSE


> sessionInfo()
R version 2.11.1 Patched (2010-05-31 r52167)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
[5] LC_TIME=English_Canada.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

P.S. I also get the same thing under:

> R.version.string
[1] "R version 2.12.0 Under development (unstable) (2010-05-31 r52164)"


More information about the R-devel mailing list