fabian.scheipl at stat.uni-muenchen.de
2008-Nov-21 16:00 UTC
[Rd] Wishlist: fix error in documentation for body<- (PR#13318)
Full_Name: Fabian Scheipl Version: 2.8.0 OS: Windows, Linux Submission from: (NULL) (138.246.7.150) It seems to me that the documentation for body<- is wrong. The help file for body(fun, envir = environment(fun)) <- value says that: value can be an expression or a list of R expressions. This produces errors however: ################################> f <- function(x){} > body(f) <- list(expression(res <- x^2),expression(return(x)))Fehler in as.function.default(c(formals(fun), value), envir) : ung?ltiges formales Argument f?r "function" ################################ The only way to assign multiple statements to a function body that I could get to work is to put all the statements into a single expression, (separated by ';' and in curly brackets): ################################ body(f) <- expression({res <- x^2; return(x)}) ################################ I think the words 'or a list of R expressions.' should be removed.
Peter Dalgaard
2008-Nov-21 22:46 UTC
[Rd] Wishlist: fix error in documentation for body<- (PR#13318)
fabian.scheipl at stat.uni-muenchen.de wrote:> Full_Name: Fabian Scheipl > Version: 2.8.0 > OS: Windows, Linux > Submission from: (NULL) (138.246.7.150) > > > It seems to me that the documentation for body<- is wrong. > The help file for > > body(fun, envir = environment(fun)) <- value > > says that: > > value can be an expression or a list of R expressions. > > This produces errors however: > > ################################ > >> f <- function(x){} >> body(f) <- list(expression(res <- x^2),expression(return(x))) > Fehler in as.function.default(c(formals(fun), value), envir) : > ung?ltiges formales Argument f?r "function" > > ################################ > > > The only way to assign multiple statements to a function body > that I could get to work is to put all the statements into a single expression, > (separated by ';' and in curly brackets): > > ################################ > > body(f) <- expression({res <- x^2; return(x)}) > > ################################ > > I think the words 'or a list of R expressions.' should be removed.It's slightly trickier than that. The value _can_ be a list, but only one of length 1. The same actually goes for expressions, but in that case there is explicit code to take only the first element. Also, the distinction between objects of mode "expression" and unevaluated expressions is unclear (here and elsewhere). > body(f) <- expression(x,y,z) > f function () x > body(f) <- list(quote(2+2)) > f function () 2 + 2 > body(f) <- list(expression(2+2)) > f function () expression(2 + 2) BTW: Don't use 'wishlist' on things that are real errors. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907