[R] Easy "Recall" to get ls(..., all.names=TRUE)?

Rolf Turner r.turner at auckland.ac.nz
Wed Mar 11 22:08:59 CET 2009


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

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list