Re: [R] dots and substitute

From: Charles C. Berry <cberry_at_tajo.ucsd.edu>
Date: Sun, 06 Apr 2008 12:50:15 -0700

On Sun, 6 Apr 2008, Simon Anders wrote:

> Hi,
>
> is there an elegant way to use 'substitute' with '...' arguments?
>
> My first try was this:
>
> > f1 <- function(...) substitute(...)
> > f1( 2+7, "foo", 3+5 )
> 2 + 7
>
> As you can see, substitute acts only on the first argument. So I tried
>
> > f2 <- function(...) substitute(list(...))
> > f2( 2+7, "foo", 3+5 )
> list(2 + 7, "foo", 3 + 5)
>
> This is now all the information I want, but it is not split into the
> individual arguments. I would like to get a character vector, i.e.
> something like
> c( "2 + 7", "\"foo\"", "3 + 5" )

Try this:

         f2 <- function(...) sapply( substitute(list(...)), deparse )[-1]

         all.equal( c( "2 + 7", "\"foo\"", "3 + 5" ), f2( 2+7, "foo", 3+5 ))

HTH, Chuck

p.s. Why do you want this as mode "character"? Warning:

> fortune(106)

If the answer is parse() you should usually rethink the question.

>

>
> My third try seems to be wrong as well:
>
> > f3 <- function(...) lapply( list(...), substitute )
> > f3( 2+7, "foo", 3+5 )
> Error in lapply(list(...), substitute) :
> '...' used in an incorrect context
>
> Does anybody has a good hint for me as how to do it correctly?
>
> Thanks
> Simon
>
>
> +---
> | Dr. Simon Anders, Dipl. Phys.
> | European Bioinformatics Institute, Hinxton, Cambridgeshire, UK
> | preferred (permanent) e-mail: sanders_at_fs.tum.de
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry_at_tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901

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. Received on Sun 06 Apr 2008 - 19:52:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 06 Apr 2008 - 23:30:27 GMT.

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

list of date sections of archive