Extract value of argument from ellipsis without evaluating other arguments

Viewed 89

Within a function, how can I extract the value of an argument from the ellipsis, without evaluating any other arguments in the ellipsis?

Concretely, how would I modify the body of this function to return "get me" from the call below?

foo <- function(...) {
    if (hasArg(bar)) {
        list(...)[["bar"]]
    }
}

foo(bar = paste("get", "me"), baz = oops)
#> Error in foo(bar = paste("get", "me"), baz = oops): object 'oops' not found
1 Answers

You can capture the call and evaluate the parameter explicitly yourself

foo <- function(...) {
  if (hasArg(bar)) {
    eval.parent(match.call()[["bar"]])
  }
}

foo(bar = paste("get", "me"), baz = oops)
# [1] "get me"
Related