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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Jan 29 19:20:32 CET 2005


Graham Jones <maillists at visiv.co.uk> writes:

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

It's mainly a design issue. Had the "..." in the definition of
integrate been further to the left, then your problem wouldn't exist,
but you wouldn't be able to use abbreviations for abs.tol and friends. 

A simple (if tedious) workaround is

integrate(f, 0, 1, a=.5, abs.tol=.Machine$double.eps^0.25, aux=NULL)

a more thoroughly effective one could be

myintegrate <- 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) 
 integrate(f, lower, upper, subdivisions, rel.tol, abs.tol, 
             stop.on.error, keep.xy, aux, ...)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list