function_ | R Documentation |
function_
is a normally-evaluating version of function
, which
creates closures. A closure object has three components: the
argument list, the body expression, and the enclosing environment.
arglist()
is a helper that produces a named list of
missing_values given a character vector of names.
function_(args, body, env = arg_env(args, environment()))
arglist(names, fill = missing_value())
args |
The argument list of the new function. NULL is accepted
to make a function with no arguments. Arguments are specified as
a named list; the list names become the argument names, and the
list values become the default expressions. A value of
|
body |
An expression for the body of the function. |
env |
The enclosing environment of the new function. |
names |
A character vector. |
fill |
The expression (default missing) |
A closure.
environment formals body
f1 <- function(x, y = x) { x + y }
f2 <- function_(alist(x = , y = x),
quote( { x + y } ),
environment())
identical(f1, f2) # TRUE
# `fn` makes a compact way to write functions;
# `fn(x+y)` is equivalent to `function(x, y) x+y`
fn <- function(exp) {
exp_ <- arg(exp)
nn <- arglist(all.names(expr(exp_), functions=FALSE))
function_(nn, expr(exp_), env(exp_))
}
fn(x^2)
fn(x+y)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.