I would like to specify a custom argument for a function created via rlang::new_function. For this I need to pass an argument to the args-argument of new_function.
I assume the named_arg should be quoted/ unquoted. This is what I have tried so far:
library(rlang)
function_factory <- function(named_arg) {
new_function(
exprs(named_arg =,... = ),
expr(print("hello world")),
caller_env()
)
}
fun1 <- function_factory(arg1)
fun1
#> function (named_arg, ...)
#> print("hello world")
The desired output would be:
#> function (arg1, ...)
#> print("hello world")
Below is one approach I have tried, but didn't work:
function_factory <- function(named_arg) {
named_arg_quo <- enquo(named_arg)
new_function(
exprs(!!named_arg_quo =,... = ),
expr(print("hello world")),
caller_env()
)
}
#> Error: <text>:21:27: unexpected '='
#> 20: new_function(
#> 21: exprs(!!named_arg_quo =
#> ^