addtoenv               package:simecol               R Documentation

_A_d_d _F_u_n_c_t_i_o_n_s _f_r_o_m _a _N_o_n-_n_e_s_t_e_d _L_i_s_t _o_f _N_a_m_e_d _F_u_n_c_t_i_o_n_s _t_o _a _C_o_m_m_o_n
_E_n_v_i_r_o_n_m_e_n_t

_D_e_s_c_r_i_p_t_i_o_n:

     Create and set an environment where functions within a non-nested
     named list of functions see each other. This function is normally
     used within other functions.

_U_s_a_g_e:

     addtoenv(L, p = parent.frame())

_A_r_g_u_m_e_n_t_s:

       L: a non-nested list of named functions.

       p: the environment where the functions are assigned to. Defaults
          to the parent frame. 

_D_e_t_a_i_l_s:

     This function is used by the `solver functions' of 'simecol'.

_V_a_l_u_e:

     The list of equations within a common environment.

_N_o_t_e:

     This is a very special function that uses environment
     manipulations. Its purpose is to `open' the access to
     interdependend functions within a common list structure (function
     list).

_S_e_e _A_l_s_o:

     'attach', 'environment'

_E_x_a_m_p_l_e_s:

     eq <- list(f1 = function(x, y)    x + y,
                f2 = function(a, x, y) a * f1(x, y)
               )

     fx <- function(eq) {
       eq <- addtoenv(eq)
       print(ls())
       print(environment(eq$f1))
       f1(3,4) + f2(1,2,3)
     }

     fx(eq)
     ## eq$f2(2,3,4)       # should give an error outside fx
     environment(eq$f2)    # should return R_GlobalEnv again

