[R] Size of an object in workspace
Horace Tso
Horace.Tso at pgn.com
Tue Apr 24 18:15:21 CEST 2007
Aaaah, wonderful. Thanks Ben. lls appears below. Should these functions be a permanent part of R? Just a thought.
H.
> lls
function (pos = 1, pat = "") {
dimx <- function(dd) if (is.null(dim(dd)))
length(dd)
else dim(dd)
lll <- ls(pos = pos, pat = pat)
cat(formatC("mode", 1, 15), formatC("class", 1, 18), formatC("name",
1, max(nchar(lll)) + 1), "size\n-----------------------------------------------------------------\n")
if (length(lll) > 0) {
for (i in 1:length(lll)) {
cat(formatC(eval(parse(t = paste("mode(", lll[i],
")"))), 1, 15), formatC(paste(eval(parse(t = paste("class(",
lll[i], ")"))), collapse = " "), 1, 18), formatC(lll[i],
1, max(nchar(lll)) + 1), " ", eval(parse(t = paste("dimx(",
lll[i], ")"))), "\n")
}
}
}
>>> "Ben Fairbank" <BEN at SSANET.COM> 4/24/2007 9:10:20 AM >>>
Here is a copy of a correspondence I had with Jim Holtman -- his
function, which I now use almost daily, does exactly what I think you
need. Could you send me a copy of Bendix Carstensen's function>
Thanks,
Ben Fairbank
From: jim holtman <jholtman at gmail.com>
To: Ben Fairbank <BEN at ssanet.com>
Date: Sep 27 2006 - 4:42pm
Thanks for the suggestion. I did.
On 9/27/06, Ben Fairbank <BEN at ssanet.com> wrote:
> Nicely done. May I suggest you post it to the list? Others probably
> would benefit also. Or maybe I am the only user dense enough not to
see
> how to write it.
>
> Ben
>
> -----Original Message-----
> From: jim holtman [mailto:jholtman at gmail.com]
> Sent: Wednesday, September 27, 2006 4:36 PM
> To: Ben Fairbank
> Subject: Re: [R] Space required by object?
>
> That was a function I wrote when I needed a similar thing.
>
> On 9/27/06, Ben Fairbank <BEN at ssanet.com> wrote:
> > Thank you! Exactly what I needed!
> >
> > Is that something you wrote?
> >
> > Ben Fairbank
> >
> > -----Original Message-----
> > From: jim holtman [mailto:jholtman at gmail.com]
> > Sent: Wednesday, September 27, 2006 4:23 PM
> > To: Ben Fairbank
> > Subject: Re: [R] Space required by object?
> >
> > try this one:
> >
Note -- Horace, the function starts here --
my.ls <- function(pos=1, sorted=F){
.result <- sapply(ls(pos=pos, all.names=TRUE),
function(..x)object.size(eval(as.symbol(..x))))
if (sorted){
.result <- rev(sort(.result))
}
.ls <-
as.data.frame(rbind(as.matrix(.result),"**Total"=sum(.result)))
names(.ls) <- "Size"
.ls$Size <- formatC(.ls$Size, big.mark=',', digits=0, format='f')
.ls$Mode <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)],
function(x)mode(eval(as.symbol(x))))), '-------')
.ls
}
And ends there
> >
> >
> > On 9/27/06, Ben Fairbank <BEN at ssanet.com> wrote:
> > > Does R provide a function analogous to LS() or str() that reports
> the
> > > storage space, on disk or in memory, required by objects?
> > >
> > > Ben Fairbank
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem you are trying to solve?
> >
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Horace Tso
Sent: Tuesday, April 24, 2007 10:59 AM
To: r-help at stat.math.ethz.ch
Subject: [R] Size of an object in workspace
Hi folks,
Is there a function to show the size of an R object? eg. in Kbytes?
Couple months ago Bendix Carstensen posted this marvelous little
function lls(), which shows all objects in the current workspace by
mode, class and 'size'. This is a wonderful enhancement to the build-in
ls() already and I now have it sourced in my Rprofile.site at startup.
The only drawback is, 'size' is just the length/dim of an object. For
matrices and data frames this is good enough. But for a list, knowing
how many elements in there doesn't help much. I need to know the
totality of the content in a common unit, eg. byte.
Thanks in advance.
Horace
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list