[R] 'programatically' list or call objects for use in a function?

Duncan Murdoch murdoch.duncan at gmail.com
Sat Sep 11 14:10:11 CEST 2010


On 11/09/2010 8:00 AM, Karl Brand wrote:
> Esteemed R users and developers,
> 
> How does one 'programatically' list or call objects for use in a function?

It depends on the function.

> 
> For example, i thought i could do something better than this:
> 
> save(A.cwb, B.cwb, C.cwb, D.cwb, E.cwb, F.cwb, file="afile.RData")
> 
> with something like these-
> 
> prfxs <- c("A", "B", "C", "D", "E", "F") #**
> save(as.name(paste(prfxs, "cwb", sep=".")), file="afile.RData")

For save, use list=paste(prfxs, "cwb", sep=".").

> 
> or this-
> 
> do.call(save, paste(prfxs, "cwb", sep="."), file="afile.RData")

do.call wants a *list* of arguments, not a single vector of character 
strings.  You could construct a list of the names of the objects you 
want to save, but that's a pain.  Just use the list= argument (which 
doesn't want a list argument :-).

> 
> Both failed.
> 
> #** And while i've got your attention- is there a 'letter equivalent' to 
> seq() which would have worked nicely for prfxs? ie., letter.seq(A:F)

Not that I know of, but LETTERS[1:6] will give you the sequence, and 
it's easy to write a function that you'd call as letter.seq("A", "F"). 
Having your function work as in your example would be messy.

Duncan Murdoch

> 
> Thoughts and suggestions sincerely appreciated,
> 
> Karl
> 
> 
>



More information about the R-help mailing list