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
Aaaah, wonderful. Thanks Ben. lls appears below. Should these functions be a permanent part of R? Just a thought. H.> llsfunction (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 tosee> 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.
Hi, Is 'object.size()' the function your are looking for ? Best, Fran?ois -----Message d'origine----- De?: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] De la part de Horace Tso Envoy??: mardi 24 avril 2007 17:59 ??: r-help at stat.math.ethz.ch Objet?: [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.
See ll() in R.oo (that is two L:s), e.g.> ll()member data.class dimension objectSize 1 author character 1 120 2 myfunc function NULL 512 3 x matrix c(3,11) 248 4 y array c(5,7,1) 264 ll() is quite flexible so you can create your on functions to query objects for whatever properties you want. It can also be passed to subset():> subset(ll(), objectSize > 250)member data.class dimension objectSize 2 myfunc function NULL 512 4 y array c(5,7,1) 264 Hope this help Henrik On 4/24/07, Horace Tso <Horace.Tso at pgn.com> wrote:> 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. >
library(gdata) ll() On 4/24/07, Horace Tso <Horace.Tso at pgn.com> wrote:> 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. >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III