7

How obtaining a characters vector containing the name of the dot-dot-dot arguments passed to a function e.g.:

test<-function(x,y,...)
{
    varnames=deparseName(substitute(list(...)))
    # deparseName does not exist, this is what I want !
    # so that I could *for example* call:

    for(elt in varnames)
       {print(varnames);}
}

v1=11
v2=10
test(12,12,v1,v2)

## would print 
#v1
#v2
6
  • 3
    check out match.call
    – chinsoon12
    Commented Jul 10, 2018 at 7:22
  • just print(list(...)) inside the function should do. See stackoverflow.com/a/5213372/4137985
    – Cath
    Commented Jul 10, 2018 at 7:26
  • @Cath. print(list(...)) does not give the expected output. It prints the content of variables.
    – beuhbbb
    Commented Jul 10, 2018 at 7:33
  • 1
    I guess print(names(list(...))) will do what you want
    – Cath
    Commented Jul 10, 2018 at 8:03
  • 1
    To be clear, despite the title this question is not asking for argument names, but names passed as arguments, e.g. for y but not x in f(x = 10, y).
    – alistaire
    Commented Jul 10, 2018 at 14:53

4 Answers 4

15

Try this:

test<-function(x,y,...)
{
  mc <- match.call(expand.dots = FALSE)
  mc$...
}

v1=11
v2=10
test(12,12,v1,v2)
[[1]]
v1

[[2]]
v2
12

You can use deparse and substitute to get what you want (see also this Q&A):

test<-function(x, y, ...)
{
    varnames=lapply(substitute(list(...))[-1], deparse)
    lapply(varnames, print)
    return(invisible())
}

test(12,12,v1,v2)
#[1] "v1"
#[1] "v2"
0
6

To extend the other answers a bit, if you just want the parameters passed to ... that are names, you can use is.name to subset the unevaluated dots before deparsing them to strings:

v1 <- 12
v2 <- 47
v3 <- "foo"

test <- function(x, y, ...){
    dots <- match.call(expand.dots = FALSE)$...
    dots <- dots[sapply(dots, is.name)]
    sapply(dots, deparse)
}

test(2, y = v1, z = v2, 1, v3)
#>    z      
#> "v2" "v3"
3

So, here's how match.call works :

  • match.call()[[1]] returns the name of the function

Then all the args are passed, so match.call()[[2]] and match.call()[[3]] return the 1 and 2nd param, with the value if there is no name (as here) :

test<-function(x,y,...) {
  list(
    match.call()[[1]],
    match.call()[[2]], 
    match.call()[[3]],
    match.call()[[4]],
    match.call()[[5]]
    )
}

v1=11
v2=10
test(12,12,v1,v2)
[[1]]
test

[[2]]
[1] 12

[[3]]
[1] 12

[[4]]
v1

[[5]]
v2

So if you need, here, the dots, you can do :

test<-function(x,y,...) {
  mc <- match.call()
  res <- c()
  for (i in 4:length(mc)){
    res <- c(res, mc[[i]])
  }
  res
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.