| locate | R Documentation |
locate starts at a given environment, and searches enclosing
environments for a name. It returns the first enclosing environment
which defines sym.
locate_ is the normally evaluating method; locate(x) is
equivalent to locate_(quo(x)) or locate_(quote(x), environment()).
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 sym is a list (of names) or a dots object, locate_(sym)
returns a list.
To locate where ... is bound, you can wrap it in parens, as
locate( (...) ).
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)
name <- expr(lval_)
target.env <- locate_(name, parent.env(env(lval_)))
assign(as.character(name), rval, envir=target.env)
}
x <- "not this one"
local({
x <- "this one"
local({
x <- "not this one either"
x %<<-% "this works like builtin <<-"
})
print(x)
})
print(x)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.