[R] Name conflicts when passing arguments for one function to another

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jan 29 19:34:16 CET 2005


On Sat, 29 Jan 2005, Graham Jones wrote:

> I am fairly new to R. I find it surprising that
>
> f <- function(x,a) {x-a}
> uniroot(f, c(0,1), a=.5)
>
> works, but
>
> integrate(f, 0, 1, a=.5)
>
> gives an error: Error in integrate(f, 0, 1, a = 0.5) : argument 4
> matches multiple formal arguments
>
> What is the best way of avoiding such surprises? Is there a way of
> telling integrate() that the 'a' argument is for f()?
>
> If I wrote my own function along the lines of uniroot() or integrate()
> is there a better way of passing on arguments?

You are being caught by partial matching. Ideally integrate would have arg 
list

function (f, lower, upper, ..., subdivisions = 100,
     rel.tol = .Machine$double.eps^0.25,
     abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
     aux = NULL)

(cf args(integrate)).  But the original author did not do it that way.
[The point is that args after ... have to be given in full.]

You can resolve this by explicitly giving abs.tol and aux:

>  integrate(f, 0, 1, abs.tol =1e-4, aux=NULL, a=.5)
1.758178e-18 with absolute error < 2.8e-15

or not using an argument name that has no meaning and is so subject to 
mis-matching.

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