How to get a derivative of a function?

How to get a derivative of the following function?

g <- expression(x^2) derivg <- D(g, 'x') derivg # 2 * x g1 <- derivg(2) # Error: could not find function "derivg" 

I want to find the derivative at x = 2.

+6
source share
1 answer

derivg is a call, not a function. To evaluate it at x = 2 , you can do

 eval(derivg, list(x = 2)) [1] 4 
+11
source

All Articles