[R] How to modify the body of a function?

William Dunlap wdunlap at tibco.com
Sun Jul 27 18:25:20 CEST 2014


I find bquote() handy for this sort of manipulation.  E.g.,
> f <- function() { 1 }
> origBody <- body(f)
> newBody <- bquote({ .(origBody) ; .(addedStuff) }, list(origBody=origBody, addedStuff=quote(function(){})))
> body(f) <- newBody
> f
function ()
{
    {
        1
    }
    function() {
    }
}
> f()
function ()
{
}
<environment: 0x2a38350>


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sun, Jul 27, 2014 at 6:41 AM, super <desolator88 at 163.com> wrote:
> Suppose that I had a function as below:
> f<-function() {
> return(1)
> }
> i want to change the body of f  to the form like this:
> f<-function(){
> 1
> function() {}
> }
> How can i do the task using body(f) or something else solutions?
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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