3

Possible Duplicate:
using substitute to get argument name with

Note that this is -different- from getting the vectors themselves with list(...) or something of that form. What I'd like to be able to do is simply 'echo' all arguments passed into ..., before any parsing is done.

Eg: I want a function that might act simply like:

f(apple, banana, car)
## --> returns c("apple", "banana", "car"), 
## ie, skips looking for the objects apple, banana, car

The closest I've gotten is

f <- function(...) {
  return( deparse( substitute( ... ) ) )
}

but this only returns the first argument 'caught' by the .... Thoughts?

Community
  • 1
  • 1
Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88

1 Answers1

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

Some explnation from ?match.call:

 1. match.call returns a call in which all of the specified arguments are specified by their full names .
 2. Here it is used to pass most of the call to another function, often model.frame. 
    Here the common idiom is that expand.dots = FALSE

Here some tests:

f(2)        # call of a static argument  
[[1]]
[1] 2

> f(x=2)  # call of setted argument
$x
[1] 2

> f(x=y)  # call of symbolic argument
$x
y
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • This doesn't work, unfortunately - if I pass in the name of an object that doesn't exist in the environment I'll get an error saying the object isn't found. I want to be able to get the name even if the object doesn't exist. – Kevin Ushey Dec 07 '12 at 19:40
  • This does the trick - looks like 'match.call()' is the magic function here. Thanks! – Kevin Ushey Dec 07 '12 at 19:51
  • @CauchyDistributedRV The title of the question was a little bit confusing. I suugest you change it , to mapping of the names of arguments – agstudy Dec 07 '12 at 19:52
  • 2
    `match.call(expand.dots = F)$\`...\`` would be slightly more idiomatic – hadley Dec 08 '12 at 00:06
  • 1
    @hadley `strict()` would tell you not to use `F` here ;-) (yes, I also need to clean up a shitload of old comments and posts...) – Joris Meys Jul 17 '17 at 18:47