Easy "Recall" to get ls(..., all.names=TRUE)?
-----Original Message----- From: Rolf Turner [mailto:r.turner at auckland.ac.nz] Sent: Wednesday, 11 March, 2009 5:09 PM To: Szumiloski, John Cc: r-help at r-project.org Subject: Re: [R] Easy "Recall" to get ls(..., all.names=TRUE)?
On 12/03/2009, at 3:16 AM, Szumiloski, John wrote:
Dear useRs, I have a utility function which is meant to be a clone of ls(), except with the option all.names=TRUE. Currently however, the function merely consists of a copy of the source code of ls(), except the default value of all.names is different. That approach has the drawback of future inconsistency if the code for ls() ever changes. No comment on whether that is likely or not; I would and do object to the current construction in principle. What I would like to do is rewrite the new function so that it does very minimal processing of its arguments, and then calls ls() with the new arguments, somewhat in the same spirit of the Recall() function. A challenge to me has been twofold, that this new ls() call has a different search path, and that ls() itself has a good deal of lazy evaluation (including possibly twice in one line, around line 11) in it. Getting my new function to work with all permutations of arguments, without merely copying the code from ls(), has been futile. I am sure I could manually enumerate the behavior for all permutations of all the arguments to ls(), and code each case individually. I am also sure I will not waste my time doing that. I was hoping, however, that there was some simple trick to allow this easily, one that I have missed. Even an explanation of why this might be a fool's errand would be quite helpful to my understanding of the intricacies of R evaluation.
Does the following do what you want?
myls <- function (all.names=TRUE,pos=-1,...)
{
xxx <- pmatch(names(list(...)),"envir",nomatch=0)
if(length(xxx) && xxx>0) return(ls(all.names=all.names,pos=pos,...))
eee <- if(pos==-1) parent.frame() else as.environment(pos)
ls(all.names=all.names,envir=eee,...)
}
cheers,
Rolf Turner
[snip legalese]
Rolf, as written that doesn't work: myls(3) just returned everything in
the top level workspace, .GlobalEnv, and not (in my case) everything in
package:graphics. However, I switched the order of the first two
arguments, and now it does the right thing in that case. I will still
need to test it further. Thanks for the suggestion. -John
Notice: This e-mail message, together with any attachme...{{dropped:12}}