[Rd] Generalising object.size

Jonathan Rougier J.C.Rougier@durham.ac.uk
Fri, 08 Jun 2001 09:33:05 +0100


Hi everyone,

Currently (R-1.2.3) object.size called with an environment tells you the
size of the environment handle but not the size of the sum of the
objects contained within the environment.  Is anyone but me interested
in the following simple generalisation:

"object.size" <- function (x, expand.envir = FALSE)
{
  if (expand.envir && is.environment(x)) {
    s <- 0
    for (i in ls(envir=x))
      s <- s + Recall(get(i, envir=x))
    s
  } else
    .Internal(object.size(x))
}

> fred <- new.env()
> assign("letters", letters, envir=fred)
> assign("numbers", 1:10, envir=fred) 
> object.size(fred)
[1] 28
> object.size(fred, TRUE)
[1] 408

As an aside, I had to use a for-loop here because Recall does not work
inside a sapply:

	sum(sapply(ls(envir=x), function(i) Recall(get(i, envir=x))))

causes an infinite recursion because the wrong function gets
"Recall"-ed.  Is there any way around this?

Cheers, Jonathan.

-- 
Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._