Re: [R] deparse(substitute(foo))

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Thu 27 Jul 2006 - 22:41:12 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Jul 27 23:59:16 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 28 Jul 2006 - 00:17:42 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.