Hi guys Thanks for all the fantastic suggestions! I didnt realise you could extract the body of a function in that manner. It looks like R always has many ways to solve a particular problem.
Cheers Rory On 9/4/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > And if f has brace brackets surrounding the body then do this: > > f <- function(x) { x*x } > deriv(body(f)[[2]], "x", func = TRUE) > > If you are writing a general function you can do this: > > e <- if (identical(body(f)[[1]], as.name("{"))) body(f)[[2]] else body(f) > deriv(e, "x", func = TRUE) > > > On 9/3/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > One improvement. This returns a function directly without having > > to create a template and filling in its body: > > > > deriv(body(f), "x", func = TRUE) > > > > On 9/3/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > > The problem is that brace brackets are not in the derivatives table. > > > Make sure you don't have any. > > > > > > On 9/3/07, Alberto Vieira Ferreira Monteiro <[EMAIL PROTECTED]> > wrote: > > > > Gabor Grothendieck wrote: > > > > > > > > > > Actually in thinking about this its pretty easy to do it without > Ryacas > > > > > too: > > > > > > > > > > Df <- f > > > > > body(Df) <- deriv(body(f), "x") > > > > > Df > > > > > > > > > This is weird. > > > > > > > > f <- function(x) { x^2 + 2*x+1 } > > > > Df <- f > > > > body(Df) <- deriv(body(f), "x") # error > > > > > > > > Also: > > > > > > > > f <- function(x) x^2 + 2 * x + 1 > > > > Df <- f > > > > body(Df) <- deriv(body(f), "x") # ok > > > > D2f <- f > > > > body(D2f) <- deriv(body(Df), "x") # error > > > > > > > > Alberto Monteiro > > > > > > > > > > [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.