[R] about body<-

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 30 14:02:45 CET 2008


Its also possible to build up a string:

> f <- function(x) {}
> body(f) <- parse(text = "{ x <- x^5; return(x) }")
> f
function (x)
{
    x <- x^5
    return(x)
}


On Sun, Nov 30, 2008 at 4:15 AM, ronggui <ronggui.huang at gmail.com> wrote:
> In  body(fun, envir = environment(fun)) <- value,  value can be an
> expression or a list of R expressions.
>
> But it seems that value must be length-1 list (if it is a list), is it right?
>> f2 <- function(x) {}
>> body(f2) <- list(quote(x<- x^5),quote(return(y)))
> Error in as.function.default(c(formals(fun), value), envir) :
>  invalid formal argument list for "function"
>
>
> I want to construct a function by computing, what I do at this moment
> is as follows:
>
>  >f <- function(x) {}
>> body(f)[[2]] <- quote(x<- x^5)
>> body(f)[[3]] <- quote(return(y))
>
> Is there a better way to do this?
>
> Thanks
>
> --
> HUANG Ronggui, Wincent
> Tel: (00852) 3442 3832
> PhD Candidate, City University of Hong Kong
> http://ronggui.huang.googlepages.com/
>
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list