[R] partial match gotcha
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Oct 20 13:40:31 CEST 2005
On Thu, 20 Oct 2005, Robin Hankin wrote:
> Hi
>
> The following gotcha took me a long time to catch:
>
> f <- function(x, main0="red", ...){
> par(col.axis=main0)
> plot(x,...)
> }
>
> f(1:10,main="title here")
> f(1:10,main="title here",main0="blue")
>
>
> I can't quite succinctly summarize why the second case works but the
> first one
> doesn't.
You need to write
f <- function(x, ..., main0="red"){
par(col.axis=main0)
plot(x,...)
}
See e.g. S Programming p.40 or the Draft R Language Definition (section
'Argument matching'). Yes, it is a trap for the unwary, which is why
knowing the exact rules is important.
Inserting print(match.call()) can help: in your first case it gives
f(x = 1:10, main0 = "title here")
--
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
More information about the R-help
mailing list