stschn
stschn

Reputation: 147

deparse and substitute on ellipsis to get names of parameters

How do I get the names of paramaters used for ellipsis in a function call, independent whether they are packed into a list or not?

The following function works pretty fine if the parameters are passed directly into the function.

foo <- function(...) {
  sapply(substitute(...()), deparse)
}

a <- 1:3
b <- 2:6
foo(a, b)

result: "a" "b"

Now I pack the parameters explicitly into a list, to make my code more pipe-friendly:

foo(list(a, b))

result: "list(a, b)"

The function foo should also work with that, to return only the names a and b. How to handle this within function foo?

Many thanks in advance.

Upvotes: 3

Views: 380

Answers (1)

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

As a workaround try this

foo <- function(...) {
    x <- substitute(...())
    if(class(x[[1]]) == "call")  sapply(x[[1]][-1] , deparse)
    else sapply(x , deparse)
}

  • Output
> foo(list(a, b))
[1] "a" "b"

> foo(a, b)
[1] "a" "b"

Upvotes: 3

Related Questions