[R] deparse(substitute(foo))
Armstrong, Whit
whit.armstrong at hcmny.com
Thu Jul 27 15:01:26 CEST 2006
That works perfectly.
Thanks,
Whit
-----Original Message-----
From: Marc Schwartz [mailto:MSchwartz at mn.rr.com]
Sent: Thursday, July 27, 2006 8:41 AM
To: Armstrong, Whit
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] deparse(substitute(foo))
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
This e-mail message is intended only for the named recipient(s) above. It may contain confidential information. If you are not the intended recipient you are hereby notified that any dissemination, distribution or copying of this e-mail and any attachment(s) is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by replying to this e-mail and delete the message and any attachment(s) from your system. Thank you.
More information about the R-help
mailing list