[R] Variable scoping question
William Dunlap
wdunlap at tibco.com
Thu Sep 8 17:53:10 CEST 2011
In my.ls() you ought to convert the pos argument
to an environment and consistently use that environment
in the calls to eval, get, and ls in the function.
E.g., with the following modification
my.ls1 <- function (pos = 1, sorted = FALSE, envir = as.environment(pos))
{
.result <- sapply(ls(envir = envir, all.names = TRUE), function(..x) object.size(eval(as.symbol(..x),
envir = envir)))
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),
envir = envir)))), "-------")
.ls
}
we get:
> sorted <- 10:1 # put a variable in .GlobalEnv that is also in my.ls*
> my.ls1()
Size Mode
my.ls 12,576 function
my.ls1 23,424 function
sorted 88 numeric
**Total 36,088 -------
> my.ls()
Size Mode
my.ls 12,576 function
my.ls1 23,424 function
sorted 48 logical
**Total 36,048 -------
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Nordlund, Dan
> (DSHS/RDA)
> Sent: Thursday, September 08, 2011 8:33 AM
> To: r-help at r-project.org
> Subject: Re: [R] Variable scoping question
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of jim holtman
> > Sent: Thursday, September 08, 2011 6:52 AM
> > To: Bos, Roger
> > Cc: r-help at r-project.org
> > Subject: Re: [R] Variable scoping question
> >
> > 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
> > }
> >
>
> Jim,
>
> I thought I would try out your function above. I copy-and-pasted the function into a newly started,
> vanilla R session, then ran the following code.
>
> > x <- 1:1000
> > my.ls()
> Size Mode
> my.ls 12,576 function
> x 4,040 character
> **Total 16,616 -------
> >
>
> I don't understand the character mode for the vector x. Any thoughts? Here is my sessionInfo().
>
> > sessionInfo()
> R version 2.13.1 (2011-07-08)
> Platform: x86_64-pc-mingw32/x64 (64-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> loaded via a namespace (and not attached):
> [1] tools_2.13.1
> >
>
>
> Dan
>
> Daniel J. Nordlund
> Washington State Department of Social and Health Services
> Planning, Performance, and Accountability
> Research and Data Analysis Division
> Olympia, WA 98504-5204
>
>
> ______________________________________________
> 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.
More information about the R-help
mailing list