landau
landau

Reputation: 5841

Capture ... passed to function without mangling the !! operator

I need to forward ... to a function argument, extract the symbols as code, and convert them to characters, all while preserving names. I usually use match.call(expand.dots = FALSE)$... for this, but it mangles the !! operator.

Current behavior

f <- function(...){
  match.call(expand.dots = FALSE)$...
}

f(a = 1234, b = !!my_variable)

## $a
## [1] 1234
## 
## $b
## !(!my_variable)

Desired behavior

f <- function(...){
  # whatever works
}

f(a = 1234, b = !!my_variable)

## $a
## [1] 1234
## 
## $b
## !!my_variable

EDIT Even better:

f <- function(...){
  # whatever works
}

my_variable <- "my value"
f(a = 1234, b = !!my_variable)

## $a
## [1] 1234
## 
## $b
## [1] "my_value"

Upvotes: 1

Views: 49

Answers (1)

landau
landau

Reputation: 5841

The following code appears to work. The use case is here. Thanks to MrFlick for nudging me in the right direction.

f <- function(...){
  rlang::exprs(...)
}

my_variable <- "my value"
f(a = 1234, b = !!my_variable)

## $a
## [1] 1234
## 
## $b
## [1] "my_value"

EDIT: by the way, I am still looking for a way to parse !! without evaluating it. This would enhance user-side functionality related to https://github.com/ropensci/drake/issues/200.

Upvotes: 2

Related Questions