An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110113/ac2231e9/attachment.pl>
question about deparse(substitute(...))
3 messages · Sean Zhang, William Dunlap, Gabor Grothendieck
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Sean Zhang
Sent: Thursday, January 13, 2011 4:37 PM
To: r-help at r-project.org
Subject: [R] question about deparse(substitute(...))
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.
One way is:
> f <- function(...) lapply(substitute(placeholderFunction(...))[-1],
deparse)
> f(x, log(y), (function(a){a+a^2/2+a^3/6})(z))
[[1]]
[1] "x"
[[2]]
[1] "log(y)"
[[3]]
[1] "(function(a) {"
[2] " a + a^2/2 + a^3/6"
[3] "})(z)"
Use paste(collapse="\n",s) or s[1] on
deparse's output if you require one
string per input.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
Thanks in advance. -Sean [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
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