Why is S4 inheritance lost between environments?
Suppose I have a class MyClass
that is defined like this:
setClass(
"MyClass",
slots = c(message = "character"),
validity = function(object) { T })
If I instantiate it, it inherits
works as expected:
myInstance <- new("MyClass", message = "Hello")
inherits(myInstance, "MyClass")
TRUE
However, it doesn't work after I put the instance in the environment and put it back again:
e <- new.env(hash = T, parent = emptyenv())
assign("MyInstance", myInstance, envir = e)
inherits(mget("MyInstance", envir = e), "MyClass")
FALSE
But the data still exists:
mget("MyInstance", envir = e)
$ MyInstance Object of class "MyClass" Slot "message": [1] "Hello"
How can I tell R to maintain my S4 classes even while saving and loading instances between environments?
source to share
mget
returns a named list of requested objects. You are actually studying the list. To examine an object, you need to extract it from the output mget
. Or just use get
that only returns the object of interest.
mget
useful when querying a bunch of objects, but if you just want it, then get
just fine.
source to share