[R] Pass character vector to function argument
Kevin Ummel
kevinummel at gmail.com
Tue Nov 16 19:04:00 CET 2010
Sorry, I shouldn't have used 'sum' as an example; I am looking for a solution in the case of functions that do not result in simple vectors or matrices.
The "real-world" example is something like the following using the 'raster' package, though (I think) any command producing an S4 object could be a candidate:
r1 <- raster(system.file("external/test.grd", package="raster"))*1
r2 <- raster(system.file("external/test.grd", package="raster"))*2
obs=objects(pattern=glob2rx("r?"))
#Would like to generate the equivalent of...
stack(r1,r2)
...using a variation of obs as an argument to 'stack' (potentially very long).
Cheers,
Kevin
On Nov 16, 2010, at 5:07 PM, David Winsemius wrote:
>
> On Nov 16, 2010, at 10:34 AM, Kevin Ummel wrote:
>
>> A bit embarrassed to post this seemingly trivial question, but I can't find anything in the archive that's quite relevant:
>>
>> a1=1
>> a2=2
>>
>> obs=objects(pattern=glob2rx("a?"))
>>
>> I want to utilize 'obs' as a function argument to produce something like:
>>
>> sum(a1,a2)
>>
>
> ?get
> > sum(sapply(obs, get))
> [1] 3
>
>
>> Obviously, sum(obs) doesn't work, but I've tried variations of 'eval', 'parse', 'mget', and 'noquote' without success. What am I missing?
>>
>> Thanks,
>> Kevin
>>
> --
> David Winsemius, MD
> West Hartford, CT
>
More information about the R-help
mailing list