Reputation:
I have the following function which takes an unquoted input and returns a character vector of the input objects.
my_function <- function(...) {
v_chr <- purrr::map_chr(rlang::enexprs(...), rlang::as_name)
v_chr
}
my_function(apple, orange)
"apple" "orange"
I am trying to build a wrapper around it that takes a character vector input. This does not work.
my_wrapper <- function(v_chr) {
x <- rlang::syms(v_chr)
my_function(x)
}
v_chr <- c("apple", "orange")
my_wrapper(v_chr)
"x"
Question
my_wrapper()
to return the same output as my_function()
?my_function()
?I would like the solution to use rlang
, but would not mind seeing the base solution in addition.
Upvotes: 0
Views: 164
Reputation: 206197
Your wrapper should look like
my_wrapper <- function(v_chr) {
x <- rlang::syms(v_chr)
rlang::eval_tidy(my_function(!!!x))
}
you need to inject those symbols into the call with the bang-bang-bang operator and then evaluate that with eval_tidy
because "normal" R functions don't recognize !!!
.
Upvotes: 1