[R] deparse(substitute(foo))

Marc Schwartz MSchwartz at mn.rr.com
Thu Jul 27 14:41:12 CEST 2006


On Thu, 2006-07-27 at 08:18 -0400, Armstrong, Whit wrote:
> I see that plot.default uses deparse(substitute(x)) to extract the
> character name of an argument and put it on the vertical axis.
> 
> Hence:
> foo <- 1:10
> plot( foo )
> 
> will put the label "foo" on the vertical axis.
> 
> However, for a function that takes a "..." list as an input, I can only
> extract the first argument name:
> 
> x <- 1:10
> y <- 10:20
> 
> foo <- function(...) {
>   print(deparse(substitute(...)))
> }
> 
> foo(x,y)
> 
> returns: 
> 
> > foo(x,y) 
> [1] "x"
> > 
> 
> and when I try to convert the list to a local variable and then extract
> names, that doesn't work either:
> 
> x <- 1:10 
> y <- 10:20 
>  
> foo <- function(...) { 
>  
>     x <- list(...) 
>     print(deparse(substitute(names(x)))) 
> } 
>  
> foo(x,y) 
> 
> returns:
> 
> > foo(x,y)
> [1] "names(list(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), c(10, 11, 12, 13, "
> [2] "14, 15, 16, 17, 18, 19, 20)))"                                  
> > 
> 
> 
> Can someone suggest a way to extract the variable names when they are
> passed as a list via "..." ?
> 
> Thanks,
> Whit

Try this:

x <- 1:10 
y <- 10:20 
 
foo <- function(...) { 
   sapply(match.call()[-1], deparse)
} 


> foo(x, y)
[1] "x" "y"


HTH,

Marc Schwartz



More information about the R-help mailing list