Skip to content
Prev 52157 / 63424 Next

Model object, when generated in a function, saves entire environment when saved

One way around this problem is to make a new environment whose
parent environment is .GlobalEnv and which contains only what the
the call to lm() requires and to compute lm() in that environment.   E.g.,

tfun1 <- function (subset)
{
    junk <- 1:1e+06
    env <- new.env(parent = globalenv())
    env$subset <- subset
    with(env, lm(Sepal.Length ~ Sepal.Width, data = iris, subset = subset))
}
Then we get
   > saveSize(tfun1(1:4)) # see below for def. of saveSize
   [1] 910
instead of the 2129743 bytes in the save file when using the naive method.

saveSize <- function (object) {
    tf <- tempfile(fileext = ".RData")
    on.exit(unlink(tf))
    save(object, file = tf)
    file.size(tf)
}



Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Wed, Jul 27, 2016 at 10:48 AM, Kenny Bell <kmb56 at berkeley.edu> wrote: