[R] obtaining output from an evaluated expression

Dieter Menne dieter.menne at menne-biomed.de
Fri Dec 19 15:21:11 CET 2008

Jack Bowden <jack.bowden <at> mrc-bsu.cam.ac.uk> writes:

>  > dU1dtheta  <- deriv(~ xi-(alpha0+alpha1*gi), c("alpha0","alpha1")) 
>  > eval(dU1dtheta)
> (Intercept) 
>  -0.2547153 
> attr(,"gradient")
>      alpha0 alpha1
> [1,]     -1      0
> I want to extract the output gradient values of -1 and 0 but I don't 
> know how to access them. The only thing I can access is the intercept  
> term via.
>  > eval(dU1dtheta)[1].

It probably easiest to ask for a function to be returned using function.arg, as
the example in deriv shows. Having been bitten by deriv in the past, I prefer to
use it to give me the function or expression, and paste it explicitly into my
code, doing some sanity check first. It's not very general, but tells you better
what is happening.

## function with defaulted arguments:
(fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
             function(b0, b1, th, x = 1:7){} ) )

More information about the R-help mailing list