locate {nseval} | R Documentation |
locate
starts at a given environment, and searches enclosing
environments for a name. It returns the first which defines sym
.
locate_
is the normally evaluating method; locate(x)
is
equivalent to locate_(quo(x))
or locate_(quote(x), environment())
.
If sym
is a list (of names) or a dots object, locate_(sym)
returns a list.
When sym
is a quotation or dots, any env
argument is ignored.
locate(sym, env = arg_env_(quote(sym), environment()), mode = "any", ...) locate_(sym, env = arg_env_(quote(sym), environment()), mode = "any", ...) locate_.list(sym, env = arg_env_(quote(sym), environment()), mode = "any", ...) locate_.quotation(sym, env = "ignored", mode = "any", ...) locate_.character(sym, env = arg_env_(quote(sym), environment()), mode = "any", ...) "locate_.("(sym, env = arg_env_(quote(sym), environment()), mode = "any", ...) locate_.dots(sym, env = "ignored", mode = "any", ...) locate_.name(sym, env = arg_env_(quote(sym), environment()), mode = "any", ifnotfound = stop("Binding ", deparse(sym), " not found"))
sym |
A name. For |
env |
Which environment to begin searching from. |
mode |
Either |
... |
Further arguments passed to methods. |
ifnotfound |
What is returned if the symbol is not found. By default an exception is raised. |
An environment object which defines sym
, if one is found.
If you use a literal character argument, as in locate("x", environment())
, you must also provide the environment argument
explicitly; locate("x")
won't work in compiled
functions. However using a literal name like locate(x)
will
work OK. See note under arg.
# Here is how to implement R's `<<-` operator, using `locate_`: `%<<-%` <- function(lval, rval) { lval_ <- arg(lval) rval_ <- arg(rval) target.env <- locate_(expr(lval_), parent.env(env(lval_))) #note that `<-` is a primitive which requires its lvalue and call #head to come from teh same env env(lval_) <- target.env do_(quo(`<-`, target.env), lval_, rval_) } x <- "not this one" local({ x <- "this one" local({ x <- "not this one either" x %<<-% "this works like builtin <<-" }) print(x) })