[R] Expressions and Functions

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 14 16:13:00 CET 2003


On Fri, 14 Nov 2003, Liaw, Andy wrote:

> You should be able to use deriv(..., func=TRUE) to get a function returned,
> instead of an expression.
> 
> If you need to use D, here's a rather clumsy way that seems to work:
> 
> > dx2x <- D(expression(x^2), name="x"); dx2x
> 2 * x
> > eval(parse(text=paste("f <- function(x){",
>                         paste(deparse(dx2x), collapse=";"), "}")))
> > f
> function(x){ 2 * x }

I would have used

f <- function(x) {}
body(f) <- D(expression(x^2), name="x")

> 
> HTH,
> Andy
> 
> > From: Erin Hodgess [mailto:hodgess at gator.uhd.edu] 
> > 
> > Dear R People:
> > 
> > When the D function is used for a symbolic derivative,
> > an expression is returned, which is fine.
> > 
> > How do you change that expression to a function, please?
> > 
> > I've been experimenting with substitute and deparse, but no 
> > success yet.
> > 
> > This is R 1.8.0 for Windows.
> > 
> > thanks in advance for the help!
> > 
> > Sincerely,
> > Erin Hodgess
> > mailto: hodgess at gator.uhd.edu
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 

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




More information about the R-help mailing list