[R] dots and substitute

Charles C. Berry cberry at tajo.ucsd.edu
Sun Apr 6 21:50:15 CEST 2008


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.
    -- Thomas Lumley
       R-help (February 2005)

>


>
> 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



More information about the R-help mailing list