[R] question about deparse(substitute(...))

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 14 13:18:15 CET 2011


On Thu, Jan 13, 2011 at 7:36 PM, Sean Zhang <seanecon at gmail.com> wrote:
> Dear R helpers:
>
> I like to apply deparse(substitute()) on multiple arguments to collect the
> names of the arguments into a character vector.
> I used function test.fun as below. it works when there is only one input
> argument. but it does not work for multiple arguements. can someone kindly
> help?
>
> test.fun <- function(...){deparse(substitute(...))}
> test.fun(x) #this works
> test.fun(x,y,z) # I like c('x','y','z') be the output, but cannot get it.
>

Try this:

> test.fun.2 <- function(...) sapply(match.call()[-1], deparse)
> test.fun.2(x, y, z)
[1] "x" "y" "z"

You could consider replacing sapply with lapply since it will return a
list of vectors if the arguments are complex -- so that would
consistently return a list.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list