Why does> curve(function(y) y^2, from=0,to=1)not work, whereas> myf <- function(y) y^2 > curve(myf, from=0,to=1)work? For the former, I get the error message Error in curve(function(y) y^2, from = 0, to = 1) : 'expr' must be a function or an expression containing 'x' I'm using R1.7.0 under Windows XP. Damon Wischik.
Clue is in the error message ... "an expression containing x". Not y. This works: curve(x^2, from=0,to=1) Think of plotting f(x), not calling it y. SF -----Original Message----- From: Damon Wischik [mailto:djw1005 at cam.ac.uk] Sent: 30 July 2003 15:11 To: R-Help Subject: [R] Plotting a function with curve() Why does> curve(function(y) y^2, from=0,to=1)not work, whereas> myf <- function(y) y^2 > curve(myf, from=0,to=1)work? For the former, I get the error message Error in curve(function(y) y^2, from = 0, to = 1) : 'expr' must be a function or an expression containing 'x' I'm using R1.7.0 under Windows XP. Damon Wischik. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
On Wed, 30 Jul 2003, Damon Wischik wrote:> > Why does > > curve(function(y) y^2, from=0,to=1) > not work, whereas > > myf <- function(y) y^2 > > curve(myf, from=0,to=1) > work? >Because someone was trying to be too clever in writing curve(). The first argument must either be an expression with the free variable `x' or the *name* of a function. You can't specify a literal function or the name of an expression. The reason is that it is hard to tell whether an argument is a function or an expression without evaluating it, and if it's an expression you may not be able to evaluate it. You could always use plot(function(y) y^2) -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle