[R] Variable scoping question
jim holtman
jholtman at gmail.com
Thu Sep 8 15:52:20 CEST 2011
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?
More information about the R-help
mailing list