I modified an example in the object.size help page to create a function I want to be able to run: "mysize" <- function() { z <- sapply(ls(), function(w) object.size(get(w))) as.matrix(rev(sort(z))[1:5]) } mysize() When I test the lines inside the function it works fine:> z <- sapply(ls(), function(w) object.size(get(w)))> as.matrix(rev(sort(z))[1:5])[,1] mat 166344288 mod 130794704 zidx 799664 wfidx 799664 megacap 799664>But when I try to run the function, it produces an error:> "mysize" <- function() {+ z <- sapply(ls(), function(w) object.size(get(w))) + as.matrix(rev(sort(z))[1:5]) + }> mysize()Error in rev(sort(z)) : error in evaluating the argument 'x' in selecting a method for function 'rev': Error in sort.int(x, na.last = na.last, decreasing decreasing, ...) : 'x' must be atomic>It must be a variable scoping problem, but I am not sure how to tackle it. Thanks, Roger *************************************************************** This message is for the named person's use only. It may\...{{dropped:14}}
On Sep 8, 2011, at 9:13 AM, Bos, Roger wrote:> I modified an example in the object.size help page to create a > function > I want to be able to run: > > "mysize" <- function() { > z <- sapply(ls(), function(w) object.size(get(w))) > as.matrix(rev(sort(z))[1:5]) > } > mysize() > > When I test the lines inside the function it works fine: > >> z <- sapply(ls(), function(w) object.size(get(w))) > >> as.matrix(rev(sort(z))[1:5]) > [,1] > mat 166344288 > mod 130794704 > zidx 799664 > wfidx 799664 > megacap 799664 >> > > But when I try to run the function, it produces an error: > >> "mysize" <- function() { > + z <- sapply(ls(), function(w) object.size(get(w))) > + as.matrix(rev(sort(z))[1:5]) > + } > >> mysize() > Error in rev(sort(z)) : > error in evaluating the argument 'x' in selecting a method for > function 'rev': Error in sort.int(x, na.last = na.last, decreasing > decreasing, ...) : > 'x' must be atomic >> > > It must be a variable scoping problem, but I am not sure how to tackle > it.I would have called it a function scoping problem. Inside the function ls() will not "see" the global environment unless you tell it where to look. (It may even be two levels deeper than the globalenv() when called inside `sapply`.) When called without an environment, it just looks in parent.frame which is inside the function. "mysize" <- function() { z <- sapply(ls(env=globalenv()), function(w) object.size(get(w))) as.matrix(rev(sort(z))[1:5]) } #works for me. -- David Winsemius, MD West Hartford, CT
Here is a function I use to look at the sizes of objects: my.ls <- function (pos = 1, sorted = FALSE) { .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 } On Thu, Sep 8, 2011 at 9:13 AM, Bos, Roger <roger.bos at rothschild.com> wrote:> I modified an example in the object.size help page to create a function > I want to be able to run: > > "mysize" <- function() { > ? ?z <- sapply(ls(), function(w) object.size(get(w))) > ? ?as.matrix(rev(sort(z))[1:5]) > } > mysize() > > When I test the lines inside the function it works fine: > >> ? ? z <- sapply(ls(), function(w) object.size(get(w))) > >> ? ? as.matrix(rev(sort(z))[1:5]) > ? ? ? ? ? ? [,1] > mat ? ? 166344288 > mod ? ? 130794704 > zidx ? ? ? 799664 > wfidx ? ? ?799664 > megacap ? ?799664 >> > > But when I try to run the function, it produces an error: > >> "mysize" <- function() { > + ? ? z <- sapply(ls(), function(w) object.size(get(w))) > + ? ? as.matrix(rev(sort(z))[1:5]) > + } > >> mysize() > Error in rev(sort(z)) : > ?error in evaluating the argument 'x' in selecting a method for > function 'rev': Error in sort.int(x, na.last = na.last, decreasing > decreasing, ...) : > ?'x' must be atomic >> > > It must be a variable scoping problem, but I am not sure how to tackle > it. > > Thanks, > > Roger > *************************************************************** > > This message is for the named person's use only. It may\...{{dropped:14}} > > ______________________________________________ > R-help at r-project.org 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 Data Munger Guru What is the problem that you are trying to solve?
Apparently Analagous Threads
- Using object.size inside a function
- How to specify a variable name in the regression formula without hard coding it
- How to Derive an S4 Class from a data.frame?
- [LLVMdev] Passing specific register for an Instruction in target description files.
- Change title size in plot(model)?