body                  package:base                  R Documentation

_A_c_c_e_s_s _t_o _a_n_d _M_a_n_i_p_u_l_a_t_i_o_n _o_f _t_h_e _B_o_d_y _o_f _a _F_u_n_c_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Get or set the body of a function.

_U_s_a_g_e:

     body(fun = sys.function(sys.parent()))
     body(fun, envir = environment(fun)) <- value

_A_r_g_u_m_e_n_t_s:

     fun: a function object, or see 'Details'.

   envir: environment in which the function should be defined.

   value: an object, usually a language object: see section 'Value'.

_D_e_t_a_i_l_s:

     For the first form, 'fun' can be a character string naming the
     function to be manipulated, which is searched for from the parent
     environment.  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.

_V_a_l_u_e:

     'body' returns the body of the function specified.  This is
     normally a language object, most often a call to '{', but it can
     also be an object (e.g. 'pi') to be the return value of the
     function.

     The replacement form sets the body of a function to the object on
     the right hand side, and (potentially) resets the environment of
     the function.  If 'value' is of class '"expression"' the first
     element is used as the body:  any additional elements are ignored,
     with a warning.

_N_o_t_e:

     Prior to R 2.9.0, list values of 'value' needed to be supplied as
     a single-element list of the list: this was undocumented prior to
     R 2.8.1 so is unlikely to actually occur.

_S_e_e _A_l_s_o:

     'alist', 'args', 'function'.

_E_x_a_m_p_l_e_s:

     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)

