[R] recursive do.call

Thomas Lumley tlumley at uw.edu
Mon Mar 14 21:57:26 CET 2011


On Tue, Mar 15, 2011 at 9:05 AM, Jeroen Ooms <jeroenooms at gmail.com> wrote:
> Hmmm I was hoping there would be a more natural way to do it. For example,
> if you actually try to call the first function with all arguments:
>
> lm(formula=dist~speed, digits=3, data=cars)
>
> R will match whatever it can, and give you a warning with the names of
> remaining unmatched arguments. The only thing I really need is instead of a
> warning take these arguments and pass them on to the next function.
>

That is not what's actually happening.  lm() has a ... argument, so
any arguments will match it.  These arguments are all passed to
lm.fit(), which also has a ... argument, but doesn't actually use it.

The problem in general is that if a function has a ... argument there
is no automatic way to tell what arguments it actually uses.

If none of the functions have ... arguments (or if you know none of
them actually uses its ... argument) then it's possible in principle,
but it still can't be done as a static expansion.  The arguments to
the second function may depend on the class of value returned by the
first function, and unless the first function is an S4 method you
can't automatically determine the return value class without running
the function.


     -thomas

>
>
> On Mon, Mar 14, 2011 at 3:34 AM, Felix Andrews <felix at nfrac.org> wrote:
>
>> It is complicated if the argument list is all mixed in together as in
>> your example. You would have to look up argument lists for possible S3
>> methods (e.g. 'digits' is an argument to print.default), and then
>> there is S4 to think about. Also, can arguments be matched by partial
>> names? Can they be given in the argument list without a name?
>>
>> My point is that you had better have a good reason to want to do it this
>> way...
>>
>> Cheers
>> Felix
>>
>>
>> On 14 March 2011 14:24, Jeroen Ooms <jeroenooms at gmail.com> wrote:
>> > I would like to define a recursive equivalent to call or do.call, which
>> takes
>> > a vector of multiple function names and 'chains' them, by greedy matching
>> of
>> > arguments down the chain. For example, I would like to be able to do:
>> >
>> > rec.do.call(c("glm","coef","print), list(formula=dist~speed, digits=3,
>> > data=cars));
>> >
>> > which would then be equivalent to:
>> >
>> > print(coef(glm(formula=dist~speed, data=cars)), digits=3);
>> >
>> > I've been playing around with a recursive function with ellipses, but I
>> > can't wrap my head around it.
>> >
>> > --
>> > View this message in context:
>> http://r.789695.n4.nabble.com/recursive-do-call-tp3353074p3353074.html
>> > Sent from the R help mailing list archive at Nabble.com.
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>>
>>
>> --
>> Felix Andrews / 安福立
>> http://www.neurofractal.org/felix/
>>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



More information about the R-help mailing list