From tag at biostat.ku.dk  Fri Apr 19 20:57:22 2013
From: tag at biostat.ku.dk (Thomas Alexander Gerds)
Date: Fri, 19 Apr 2013 20:57:22 +0200
Subject: [Rd] how to control the environment of a formula
In-Reply-To: <51714ED0.3010307@gmail.com> (Duncan Murdoch's message of "Fri,
	19 Apr 2013 10:04:00 -0400")
References: <86bo9ce87b.fsf@biostat.ku.dk> <516FE87F.90906@gmail.com>
	<86obdbetlz.fsf@biostat.ku.dk> <51714ED0.3010307@gmail.com>
Message-ID: <86wqry5ox9.fsf@biostat.ku.dk>


hmm. I have tested a bit more, and found this perhaps more difficult
solve situation. even though I delete x, since x is part of the output
of the formula, the size of the object is twice as much as it should be:

test <- function(x){
  x <- rnorm(1000000)
  out <- list(x=x)
  rm(x)
  out$f <- as.formula(a~b)
  out
}
v <- test(1)
x <- rnorm(1000000)
save(v,file="~/tmp/v.rda")
save(x,file="~/tmp/x.rda")
system("ls -lah ~/tmp/*.rda")

-rw-rw-r-- 1 tag tag  15M Apr 19 20:52 /home/tag/tmp/v.rda
-rw-rw-r-- 1 tag tag 7,4M Apr 19 20:52 /home/tag/tmp/x.rda

can you solve this as well?

thanks!
thomas

Duncan Murdoch <murdoch.duncan at gmail.com> writes:

> On 13-04-18 11:39 AM, Thomas Alexander Gerds wrote:
>> Dear Duncan
>> thank you for taking the time to answer my questions! It will be
>> quite some work to delete all the objects generated inside the
>> function ... but if there is no other way to avoid a large
>> environment then this is what I will do.
>
> It's not really that hard.  Use names <- ls() in the function to get a
> list of all of them; remove the names of variables that might be
> needed in the formula (and the name of the formula itself); then use
> rm(list=names) to delete everything else just before returning it.
>
> Duncan Murdoch
>
-- 
Thomas A. Gerds -- Assoc. Prof. Department of Biostatistics Copenhagen
University of Copenhagen, Oester Farimagsgade 5, 1014 Copenhagen, Denmark



[Rd] how to control the environment of a formula
Thomas Alexander Gerds