Examples for 'base::body'


Access to and Manipulation of the Body of a Function

Aliases: body body<-

Keywords: programming

### ** Examples

body(body)
{
    if (is.character(fun)) 
        fun <- get(fun, mode = "function", envir = parent.frame())
    .Internal(body(fun))
}
f <- function(x) x^5
body(f) <- quote(5^x)
## or equivalently  body(f) <- expression(5^x)
f(3) # = 125
[1] 125
body(f)
5^x
## creating a multi-expression body
e <- expression(y <- x^2, return(y)) # or a list
body(f) <- as.call(c(as.name("{"), e))
f
function (x) 
{
    y <- x^2
    return(y)
}
<environment: 0x55ccff42e778>
f(8)
[1] 64
## Using substitute() may be simpler than 'as.call(c(as.name("{",..)))':
stopifnot(identical(body(f), substitute({ y <- x^2; return(y) })))

[Package base version 4.2.3 Index]