This constructs a new function given its three components: list of arguments, body code and parent environment.
new_function(args, body, env = caller_env())
| args | A named list or pairlist of default arguments. Note
that if you want arguments that don't have defaults, you'll need
to use the special function |
|---|---|
| body | A language object representing the code inside the
function. Usually this will be most easily generated with
|
| env | The parent environment of the function, defaults to the
calling environment of |
#> [1] TRUE# Pass a list or pairlist of named arguments to create a function # with parameters. The name becomes the parameter name and the # argument the default value for this parameter: new_function(list(x = 10), quote(x))#> function (x = 10) #> x #> <environment: 0x555826e6b5e0>#> function (x = 10) #> x #> <environment: 0x555826e6b5e0>#> function (x = 10) #> x #> <environment: 0x555826e6b5e0>#> function (x = 5 + 5) #> x #> <environment: 0x555826e6b5e0># Pass empty arguments to omit defaults. `list()` doesn't allow # empty arguments but `pairlist2()` does: new_function(pairlist2(x = , y = 5 + 5), quote(x + y))#> function (x, y = 10) #> x + y #> <environment: 0x555826e6b5e0>#> function (x, y = 5 + 5) #> x + y #> <environment: 0x555826e6b5e0>