Displaying 3 results from an estimated 3 matches for "normal_ggplot".
2020 Jan 29
2
Model object, when generated in a function, saves entire environment when saved
...qsave(object, file = tf)
file.size(tf)
}
save_size_rds <- function (object) {
tf <- tempfile(fileext = ".rds")
on.exit(unlink(tf))
saveRDS(object, file = tf)
file.size(tf)
}
normal_lm <- function(){
junk <- 1:1e+08
lm(Sepal.Length ~ Sepal.Width, data = iris)
}
normal_ggplot <- function(){
junk <- 1:1e+08
ggplot2::ggplot()
}
clean_lm <- function () {
junk <- 1:1e+08
# Run the lm in its own environment
env <- new.env(parent = globalenv())
env$subset <- subset
with(env, lm(Sepal.Length ~ Sepal.Width, data = iris))
}
# The qs save size in...
2020 Jan 29
0
Model object, when generated in a function, saves entire environment when saved
...ence vectors. If you replace the line
junk <- 1:1e+08
with
junk <- runif(1e+08)
you'll see drastically different results:
> save_size_qs(normal_lm())
[1] 417953609
> #> [1] 848396
> save_size_rds(normal_lm())
[1] 532614827
> #> [1] 4163
> save_size_qs(normal_ggplot())
[1] 417967987
> #> [1] 857446
> save_size_rds(normal_ggplot())
[1] 532624477
> #> [1] 12895
Duncan Murdoch
2016 Jul 27
2
Model object, when generated in a function, saves entire environment when saved
Another solution is to only save the parts of the model object that
interest you. As long as they don't include the formula (which is
what drags along the environment it was created in), you will
save space. E.g.,
tfun2 <- function(subset) {
junk <- 1:1e6
list(subset=subset, lm(Sepal.Length ~ Sepal.Width, data=iris,
subset=subset)$coef)
}
saveSize(tfun2(1:4))
#[1] 152
Bill