6

How do I get the 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.

Rich Scriven
  • 90,041
  • 10
  • 148
  • 213

1 Answers1

11

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

eval(derivg, list(x = 2))
[1] 4
Rich Scriven
  • 90,041
  • 10
  • 148
  • 213