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

From: Armstrong, Whit <whit.armstrong_at_hcmny.com>
Date: Thu 27 Jul 2006 - 23:01:26 EST


That works perfectly.

Thanks,
Whit  

-----Original Message-----
From: Marc Schwartz [mailto:MSchwartz@mn.rr.com] Sent: Thursday, July 27, 2006 8:41 AM
To: Armstrong, Whit
Cc: r-help@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.



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:08:27 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:35 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.