[R] Plotting a function with curve()

Thomas Lumley tlumley at u.washington.edu
Wed Jul 30 18:14:25 CEST 2003


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




More information about the R-help mailing list