An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130312/66716b1b/attachment.pl>
ls() with different defaults: Solution;
5 messages · Gabor Grothendieck, Hadley Wickham, Szumiloski, John +1 more
On Tue, Mar 12, 2013 at 12:59 PM, Szumiloski, John
<john_szumiloski at merck.com> wrote:
Dear useRs, Some time ago I queried the list as to an efficient way of building a function which acts as ls() but with a different default for all.names: http://tolstoy.newcastle.edu.au/R/e6/help/09/03/7588.html I have struck upon a solution which so far has performed admirably. In particular, it uses ls() and not its explicit source code, so only has a dependency on its name and the name of its all.names argument. Here is my solution: lsall <- function(...) { thecall <- as.call(c(as.name('ls'), list(...))) newcall <- match.call(definition=ls, call=thecall) if( !('all.names' %in% names(newcall)) ) newcall[['all.names']] <- TRUE eval(newcall, envir=parent.frame()) }#### end lsall In my hands this function has always acted exactly as I expected, identically to ls() but with the default of all.names=TRUE (which can be overridden as usual). In particular, it (i) gets the proper search path position right; (ii) works as expected within a browser() session. I am sharing this in case someone finds this construction helpful for other purposes, and in case someone finds a failing case they then have the option of communicating it back here.
Also have a look at the Defaults package, e.g. library(Defaults) ls2 <- ls setDefaults(ls2, all.names = TRUE) # test .a <- 1 ls() ls2() -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On Tue, Mar 12, 2013 at 12:59 PM, Szumiloski, John
<john_szumiloski at merck.com> wrote:
Dear useRs, Some time ago I queried the list as to an efficient way of building a function which acts as ls() but with a different default for all.names: http://tolstoy.newcastle.edu.au/R/e6/help/09/03/7588.html I have struck upon a solution which so far has performed admirably. In particular, it uses ls() and not its explicit source code, so only has a dependency on its name and the name of its all.names argument. Here is my solution: lsall <- function(...) { thecall <- as.call(c(as.name('ls'), list(...))) newcall <- match.call(definition=ls, call=thecall) if( !('all.names' %in% names(newcall)) ) newcall[['all.names']] <- TRUE eval(newcall, envir=parent.frame()) }#### end lsall
Why not just do:
lsall <- function(..., all.names = TRUE) {
ls(..., all.names = all.names)
}
? Then the function practically documents itself.
Hadley
Chief Scientist, RStudio http://had.co.nz/
-----Original Message----- From: Hadley Wickham [mailto:h.wickham at gmail.com] Sent: Tuesday, 12 March, 2013 1:34 PM To: Szumiloski, John Cc: r-help at r-project.org Subject: Re: [R] ls() with different defaults: Solution;
On Tue, Mar 12, 2013 at 12:59 PM, Szumiloski, John <john_szumiloski at merck.com> wrote:
Dear useRs, Some time ago I queried the list as to an efficient way of building a function which acts as ls() but with a different default for all.names: http://tolstoy.newcastle.edu.au/R/e6/help/09/03/7588.html I have struck upon a solution which so far has performed admirably. In particular, it uses ls() and not its explicit source code, so only has a dependency on its name and the name of its all.names argument. Here is my solution: lsall <- function(...) { thecall <- as.call(c(as.name('ls'), list(...))) newcall <- match.call(definition=ls, call=thecall) if( !('all.names' %in% names(newcall)) ) newcall[['all.names']] <- TRUE eval(newcall, envir=parent.frame()) }#### end lsall
Why not just do:
lsall <- function(..., all.names = TRUE) {
ls(..., all.names = all.names)
}
? Then the function practically documents itself.
The search path of the internal ls() is not the same as that of the called lsall(). You then get (e.g.)
lsall2()
[1] "..." "all.names" John Hadley -- Chief Scientist, RStudio http://had.co.nz/ Notice: This e-mail message, together with any attachme...{{dropped:11}}
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Szumiloski, John Sent: Tuesday, March 12, 2013 10:39 AM To: Hadley Wickham Cc: r-help at r-project.org Subject: Re: [R] ls() with different defaults: Solution; -----Original Message----- From: Hadley Wickham [mailto:h.wickham at gmail.com] Sent: Tuesday, 12 March, 2013 1:34 PM To: Szumiloski, John Cc: r-help at r-project.org Subject: Re: [R] ls() with different defaults: Solution; On Tue, Mar 12, 2013 at 12:59 PM, Szumiloski, John <john_szumiloski at merck.com> wrote:
Dear useRs, Some time ago I queried the list as to an efficient way of building a
function which acts as ls() but with a different default for all.names:
http://tolstoy.newcastle.edu.au/R/e6/help/09/03/7588.html I have struck upon a solution which so far has performed admirably.
In particular, it uses ls() and not its explicit source code, so only has a dependency on its name and the name of its all.names argument. Here is my solution:
lsall <- function(...) {
thecall <- as.call(c(as.name('ls'), list(...)))
newcall <- match.call(definition=ls, call=thecall)
if( !('all.names' %in% names(newcall)) ) newcall[['all.names']]
<- TRUE
eval(newcall, envir=parent.frame()) }#### end lsall
Why not just do:
lsall <- function(..., all.names = TRUE) {
ls(..., all.names = all.names)
}
? Then the function practically documents itself.
The search path of the internal ls() is not the same as that of the
called lsall(). You then get (e.g.)
lsall2()
[1] "..." "all.names" John
Then how about
lsall <- function(..., all.names = TRUE) {
ls(..., all.names = all.names, envir=parent.frame())
}
Hope this is helpful,
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