body(fun = sys.function(sys.parent()))
body(fun, envir = environment(fun)) <- value
fun
can be a character string
naming the function to be manipulated, which is searched for from the
parent frame. If it is not specified, the function calling
body
is used. The bodies of all but the simplest are braced expressions, that is
calls to {
: see the ‘Examples’ section for how to
create such a call.
alist
,
args
,
function
.
body(body)
f <- function(x) x^5
body(f) <- quote(5^x)
## or equivalently body(f) <- expression(5^x)
f(3) # = 125
body(f)
## creating a multi-expression body
e <- expression(y <- x^2, return(y)) # or a list
body(f) <- as.call(c(as.name("{"), e))
f
f(8)
## Using substitute() may be simpler than 'as.call(c(as.name("{",..)))':
stopifnot(identical(body(f), substitute({ y <- x^2; return(y) })))
Run the code above in your browser using DataLab