From kmb56 at berkeley.edu  Wed Jul 27 19:48:06 2016
From: kmb56 at berkeley.edu (Kenny Bell)
Date: Wed, 27 Jul 2016 10:48:06 -0700
Subject: [Rd] Model object, when generated in a function,
 saves entire environment when saved
Message-ID: <CALOjXYQq6bCW9V3vw7E3up05AOy54_V5YdHMEVsNCcdL-VB=1w@mail.gmail.com>

In the below, I generate a model from an environment that isn't
.GlobalEnv with a large object that is unrelated to the model
generation. It seems to save the irrelevant object unnecessarily. In
my actual use case, I am running and saving many models in a loop that
each use a single large data.frame (that gets collapsed into a small
data.frame for estimation), so removing it isn't an option.

In the case where the model exists in .GlobalEnv, everything is
peachy. So replicating whatever happens when saving the model that was
generated in .GlobalEnv at the return() stage of the function call
would fix this problem.

I was referred to this list from r-bugs. First time r-devel poster.

Hope this helps,

Kendon

```
tmp_fun <- function(x){
  iris_big <- lapply(1:10000, function(x) iris)
  lm(Sepal.Length ~ Sepal.Width, data = iris)
}

out <- tmp_fun(1)
object.size(out)
# 48008
save(out, file = "tmp.RData", compress = FALSE)
file.size("tmp.RData")
# 57196752 - way too big

# Works fine when in .GlobalEnv
iris_big <- lapply(1:10000, function(x) iris)
out <- lm(Sepal.Length ~ Sepal.Width, data = iris)

object.size(out)
# 48008
save(out, file = "tmp.RData", compress = FALSE)
file.size("tmp.RData")
# 16641 - good size.
```

	[[alternative HTML version deleted]]



[Rd] Model object, when generated in a function, saves entire environment when saved
Kenny Bell