[R] Pass character vector to function argument

Kevin Ummel kevinummel at gmail.com
Tue Nov 16 20:09:04 CET 2010


Thanks, David. That does, indeed, work. It didn't occur to me that a list would do the job as an argument.

Thanks for the fix!

kevin


On Nov 16, 2010, at 6:58 PM, David Winsemius wrote:

> 
> On Nov 16, 2010, at 1:04 PM, Kevin Ummel wrote:
> 
>> 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)
> 
> Have you tried what would seem to be the obvious generalization of the example offered:
> 
> str(stack(sapply(obs, get)))   # it does not throw any error and seems to have data in it
> 
>> 
>> ...using a variation of obs as an argument to 'stack' (potentially very long).
> 
> Also tested:
> 
> str(
> stack( c(sapply(obs, get),sapply(obs, get)) )  # doing a four object test
> )  # no error
> 
> (I did not see any indication in the help page for get that it's operation was restricted to S3 objects. If there is such a restriction then warnings should be added there.)
> 
> 
>> 
>> 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
>>> 
>> 
> 
> David Winsemius, MD
> West Hartford, CT
> 



More information about the R-help mailing list