Re: [R] partial match gotcha

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 20 Oct 2005 - 21:40:31 EST

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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Oct 20 21:47:11 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:07:42 EST