Examples for 'rlang::new_function'


Create a function

Aliases: new_function

Keywords:

### ** Examples

f <- function() letters
g <- new_function(NULL, quote(letters))
identical(f, g)
[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: 0x55ccfe314b10>
new_function(pairlist2(x = 10), quote(x))
function (x = 10) 
x
<environment: 0x55ccfe314b10>
# Use `exprs()` to create quoted defaults. Compare:
new_function(pairlist2(x = 5 + 5), quote(x))
function (x = 10) 
x
<environment: 0x55ccfe314b10>
new_function(exprs(x = 5 + 5), quote(x))
function (x = 5 + 5) 
x
<environment: 0x55ccfe314b10>
# 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: 0x55ccfe314b10>
new_function(exprs(x = , y = 5 + 5), quote(x + y))
function (x, y = 5 + 5) 
x + y
<environment: 0x55ccfe314b10>

[Package rlang version 1.1.4 Index]