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)

