[Rd] save/load + all.equal on reference class objects

Ben Bolker bbolker at gmail.com
Tue Aug 2 22:53:26 CEST 2016

 After I save an object that contains reference class objects in some of
its slots to a file and then re-load it, all.equal() seems to break for
me.  Is this a bug in all.equal, or is it likely caused by bad
implementation of methods on my side?  (I see that "'all.equal()' gains
methods for 'environment's and 'refClass'es" for R 3.2.0, but that was a
little while ago ...)  I haven't ever noticed loading these objects from
files to be problematic before.

fn <- "savecopy.rda"
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)

'pp' is one of the slots in this S4 object; it contains a reference
class object.  (The same thing happens for the other reference class
slot, 'resp', which is of a different RC ...)

all.equal(fm1 at pp,fm1 at pp$copy())

   Error in as.character.factor(target) : attempting to coerce non-factor

  Digging into this a bit I can see that all.equal() is attempting to
compare a 'current' and 'target' that both look like this:

Formal class 'ordered' [package "methods"] with 1 slot
  ..@ .S3Class: chr [1:2] "ordered" "factor"
Browse[1]> current
Object of class "ordered"

 ... but I can't really make much more sense of it than that.

  In contrast, this works fine:

fm2 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
all.equal(fm2 at pp,fm2 at pp$copy())  ## TRUE

  Any thoughts from someone wiser than I am about this stuff?

More information about the R-devel mailing list