Setzer.Woodrow@epamail.epa.gov
2003-Apr-04 22:05 UTC
[R] creating function bodies using body()
I'm having trouble figuring out how to create a function using "body<-" (). The help file for body() says that the argument should be a list of R expressions. However if I try that I get an error:> tmpfun <- function(a, b=2){} > body(tmpfun) <- list(expression(z <- a + b),expression(z^2))Error in as.function.default(c(formals(f), value), envir) : invalid formal argument list for "function" Can someone give me a simple example for doing this? Thanks! R. Woodrow Setzer, Jr. Phone: (919) 541-0128 Experimental Toxicology Division Fax: (919) 541-4284 Pharmacokinetics Branch NHEERL B143-05; US EPA; RTP, NC 27711
This apparently worked:> body(f) <- expression({x^2; x+3}) > ffunction (x) { x^2 x + 3 } HTH, Andy> -----Original Message----- > From: Setzer.Woodrow at epamail.epa.gov > [mailto:Setzer.Woodrow at epamail.epa.gov] > Sent: Friday, April 04, 2003 5:06 PM > To: r-help at stat.math.ethz.ch > Subject: [R] creating function bodies using body() > > > I'm having trouble figuring out how to create a function > using "body<-" > (). The help file for body() says that the argument should > be a list of > R expressions. However if I try that I get an error: > > > tmpfun <- function(a, b=2){} > > body(tmpfun) <- list(expression(z <- a + b),expression(z^2)) > Error in as.function.default(c(formals(f), value), envir) : > invalid formal argument list for "function" > > Can someone give me a simple example for doing this? Thanks! > > R. Woodrow Setzer, Jr. Phone: (919) 541-0128 > Experimental Toxicology Division Fax: (919) 541-4284 > Pharmacokinetics Branch > NHEERL B143-05; US EPA; RTP, NC 27711 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------
Defining a function is much simpler than that: tmpfun <- function(a, b=2) { z <- a = b z^2 } # returns (a + b)^2 But maybe you want the advertised syntax to work. Please pardon if I've misjudged the depth of your question. I've never tried to use body(). - tom blackwell - u michigan medical school - ann arbor - On Fri, 4 Apr 2003 Setzer.Woodrow at epamail.epa.gov wrote:> I'm having trouble figuring out how to create a function using "body<-" > (). The help file for body() says that the argument should be a list of > R expressions. However if I try that I get an error: > > > tmpfun <- function(a, b=2){} > > body(tmpfun) <- list(expression(z <- a + b),expression(z^2)) > Error in as.function.default(c(formals(f), value), envir) : > invalid formal argument list for "function" > > Can someone give me a simple example for doing this? Thanks! > > R. Woodrow Setzer, Jr. Phone: (919) 541-0128 > Experimental Toxicology Division Fax: (919) 541-4284 > Pharmacokinetics Branch > NHEERL B143-05; US EPA; RTP, NC 27711
Setzer.Woodrow at epamail.epa.gov writes:> I'm having trouble figuring out how to create a function using "body<-" > (). The help file for body() says that the argument should be a list of > R expressions. However if I try that I get an error: > > > tmpfun <- function(a, b=2){} > > body(tmpfun) <- list(expression(z <- a + b),expression(z^2)) > Error in as.function.default(c(formals(f), value), envir) : > invalid formal argument list for "function" > > Can someone give me a simple example for doing this? Thanks!Like this:> tmpfun <- function(a, b=2){} > body(tmpfun) <- quote({z <- a + b; z^2}) > tmpfun(3)[1] 25 And yes, that help page could do with a rewrite... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907