A colleague asked me the following question the other day. In the following piece of code, how do you extract the gradient:
> x=5 > a = eval(deriv(~ x^3, \
Though it is no better than your method, you could make a function, grad, that takes a numeric with a gradient attribute and returns the gradient value.
grad
grad = function(x)attr(x,"gradient")[1] grad(a)
which is now reusable.