[R] Problme with <<- (was Apparent namespace problem)

Luke Tierney luke at stat.uiowa.edu
Wed Apr 30 02:40:55 CEST 2003

On Mon, 28 Apr 2003, Ross Boylan wrote:

> > > First, is there a way to find out what frame (as in frames in
> > > environments, not data frames) a name is being obtained from or put
> > > into?
> > 
> > ?find.
> Here's where I have a question.  find appears to operate on the search
> path.  I thought that was a different concept from environments and
> frames.  The search path, I think, doesn't include dynamically created
> frames from the course of function execution.

Don't recall if there is anything built in, but you can use parent.env
to walk up the environment frames.  Using that, something like

findVarEnv<-function(name, env = parent.frame()) {
    while (! is.null(env)) 
        if (exists(name, env = env, inherits = FALSE)) return(env)
        else env <- parent.env(env)
    if (exists(name, env=NULL)) return(NULL)
    else return(NA)

should return the first environment in which a variable identified by
the 'name' argument exists.


Luke Tierney
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
   Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

More information about the R-help mailing list