[R] Plotting a function with curve()

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jul 30 19:06:31 CEST 2003


Thomas Lumley <tlumley at u.washington.edu> writes:

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

Actually, it wouldn't be too hard to use identical(sexpr[[1]],
as.name("function")) and detect an explicit function definition, so
someone could have been even more clever...

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