[R] Variable scoping question

Bos, Roger roger.bos at rothschild.com
Thu Sep 8 15:13:16 CEST 2011


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}}



More information about the R-help mailing list