From ripley at stats.ox.ac.uk  Tue Jan 29 06:29:38 2008
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 29 Jan 2008 05:29:38 +0000 (GMT)
Subject: [Rd] A "safe" do.call
In-Reply-To: <f8e6ff050801281830m3a46e884j715611fed13cf3a8@mail.gmail.com>
References: <f8e6ff050801281819l223d85deu458c67fd6e0b3693@mail.gmail.com>
	<f8e6ff050801281830m3a46e884j715611fed13cf3a8@mail.gmail.com>
Message-ID: <alpine.LFD.1.00.0801290525250.23305@gannet.stats.ox.ac.uk>

On Mon, 28 Jan 2008, hadley wickham wrote:

> Maybe this function won't actually be the help I had hoped it would
> be.  Unfortunately some functions (e.g. glm via glm.control) throw
> errors when ... contain arguments that don't match some (eventual)
> argument list.
>
> Or is this a bug in glm?  It certainly seems that the documentation
> should mention that ... is passed to glm.control, which only takes
> three arguments.  I realise that this doesn't come up very often
> during an interactive model fitting session, and it is easy to remedy
> when it does, but it makes writing robust functions hard when a
> function with ... does in fact have a fixed argument list.

I think the docmentation has conflated '...' for glm and '...' for 
weights.

My recollection is that this was intentional: at least one core developer 
used to dislike '...' as it allowed mistyped argument names to be ignored.
And he has a good point, IMO.

>
> Hadley
>
> On Jan 28, 2008 8:19 PM, hadley wickham <h.wickham at gmail.com> wrote:
>> Has anyone developed a version of do.call that is safe in the sense
>> that it silently drops parameters that do not appear in the formals of
>> the called function? This is useful when ... ends up being used in
>> multiple further functions.  e.g.
>>
>> f <- function(a, b) {a + b}
>> do.call(f, list(a=1, b=2, c=3))  # Errors
>> safe.call(f, list(a=1, b=2, c=3)) # Returns 3
>>
>> If have quickly thrown together the following, but it doesn't support
>> position based calls, and I'd like to avoid reinventing the wheel.
>>
>> safe.call <- function(f, params, f.params = names(formals(f))) {
>>   if ("..." %in% f.params) {
>>     safe.params <- params
>>   } else {
>>     safe.params <- params[intersect(f.params, names(params))]
>>   }
>>   do.call(f, safe.params)
>> }
>>
>> I hope to use safe.call to prevent a wide class of potential bugs in ggplot2.
>>
>> Hadley
>>
>> --
>> http://had.co.nz/
>>
>
>
>
> --
> http://had.co.nz/
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] A "safe" do.call
Prof Brian Ripley