user10917479
user10917479

Reputation:

Character vector to ellipsis input

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

  1. How to I get my_wrapper() to return the same output as my_function()?
  2. BONUS: Is there a more streamlined way to achieve the results in my_function()?

I would like the solution to use rlang, but would not mind seeing the base solution in addition.

Upvotes: 0

Views: 164

Answers (1)

MrFlick
MrFlick

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

Related Questions