Stable lifecycle

This constructs a new function given its three components: list of arguments, body code and parent environment.

new_function(args, body, env = caller_env())



A named list of default arguments. Note that if you want arguments that don't have defaults, you'll need to use the special function alist, e.g. alist(a = , b = 1)


A language object representing the code inside the function. Usually this will be most easily generated with base::quote()


The parent environment of the function, defaults to the calling environment of new_function()


f <- function(x) x + 3 g <- new_function(alist(x = ), quote(x + 3)) # The components of the functions are identical identical(formals(f), formals(g))
#> [1] TRUE
#> [1] TRUE
#> [1] TRUE
# But the functions are not identical because f has src code reference identical(f, g)
#> [1] TRUE
attr(f, "srcref") <- NULL # Now they are: stopifnot(identical(f, g))