How to pass expression variable to curve () as its equation?

I have the following code:

e <- expression(x^2+3*x-3) 

I want to draw a graph of the first derivative using the character function D of the character D:

 curve(D(e), from=0, to=10) 

But then I get the following error:

 Error in curve(expression(e), xname = "x", from = 0, to = 3000) : 'expr' must be a function, or a call or an expression containing 'x' 

I tried wrapping D (e) in an eval () call, but to no avail.

Try a little more:

 substitute(expression(x^2+3*x-3), list(x=3)) 

as expected in:

  expression(3^2+3*3-3) 

But:

  substitute(e, list(x=3)) 

leads to:

  e 

What's happening? How can I make this work?

+7
r
source share
2 answers

It's a little awkward but

 eval(substitute(curve(y),list(y=D(e,"x")))) 

seems to work. Thus,

 do.call(curve,list(D(e,"x"))) 
+5
source share
Functions

easier to manipulate and test:

 e <- expression(x^2+3*x-3) de <- D(e, 'x') fde <- function(x) eval(de) curve(fde, from=0, to=10) 
+3
source share

All Articles