Graham Jones
2005-Jan-29 13:15 UTC
[R] Name conflicts when passing arguments for one function to another
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? -- Graham Jones, author of SharpEye Music Reader http://www.visiv.co.uk 21e Balnakeil, Durness, Lairg, Sutherland, IV27 4PT, Scotland, UK
Peter Dalgaard
2005-Jan-29 18:20 UTC
[R] Name conflicts when passing arguments for one function to another
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
Prof Brian Ripley
2005-Jan-29 18:34 UTC
[R] Name conflicts when passing arguments for one function to another
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