I have a complicated list object, the output of a modelling function (asreml). The object contains all sorts of data types, including functions and formulas, which have environments attached. I don't want to save the environments to RDS, because they are quite big and I save a lot of models.
I came across the parameter refhook=
in the serialize
and saveRDS
functions. The documentation says:
The refhook functions can be used to customize handling of non-system reference objects (all external pointers and weak references, and all environments other than namespace and package environments and .GlobalEnv). The hook function for serialize should return a character vector for references it wants to handle; otherwise it should return NULL.
Given this example model
e <- new.env()
e$a = rnorm(10)
l <- list(a = e, b = 42)
The refhook function indeed show some effect. The output gets smaller when I define a function which returns a character, indicating that the environment does not get saved:
length(serialize(l, connection = NULL))
[1] 338
s <- serialize(l,
connection = NULL,
refhook = function(x) "")
length(s)
[1] 109
However, I cannot read in the resulting object:
unserialize(s)
Error in unserialize(s) :
no restore method available
I also tried a raw vector output, suspecting that maybe refhook is expected to provide an alternative serialized output, but that won't work:
s2 <- serialize(l,
connection = NULL,
refhook = function(x)
serialize("env", connection = NULL)))
Error in serialize(l, con = NULL, refhook = function(x) serialize("env", :
assertion 'TYPEOF(t) == STRSXP && LENGTH(t) > 0' failed: file 'serialize.c', line 982
How do I use refhook=
? What character output is expected from this function?