delayedAssign              package:base              R Documentation

_D_e_l_a_y _E_v_a_l_u_a_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     'delayedAssign' creates a _promise_ to evaluate the given
     expression if its value is requested.  This provides direct access
      to the _lazy evaluation_ mechanism used by R for the evaluation 
     of (interpreted) functions.

_U_s_a_g_e:

     delayedAssign(x, value, eval.env = parent.frame(1), 
                   assign.env = parent.frame(1))

_A_r_g_u_m_e_n_t_s:

       x: a variable name (given as a quoted string in the function
          call)

   value: an expression to be assigned to 'x'

eval.env: an environment in which to evaluate 'value'

assign.env: an environment in which to assign 'x'

_D_e_t_a_i_l_s:

     Both 'eval.env' and 'assign.env' default to the currently active
     environment.

     The expression assigned to a promise by 'delayedAssign' will not
     be evaluated until it is eventually ``forced''. This happens when
     the variable is first accessed.

     When the promise is eventually forced, it is evaluated within the
     environment specified by 'eval.env' (whose contents may have
     changed in the meantime).  After that, the value is fixed and the
     expression will not be evaluated again.

     This function is meant to replace the 'delay()' function, to make
     it more difficult for R code to see ``naked'' promises.

_V_a_l_u_e:

     This function is invoked for its side effect, which is assigning a
     promise to evaluate 'value' to the variable 'x'.

_S_e_e _A_l_s_o:

     'substitute', to see the expression associated with a promise.

_E_x_a_m_p_l_e_s:

     msg <- "old"
     delayedAssign("x", msg)
     msg <- "new!"
     x #- new!
     substitute(x) #- msg

     delayedAssign("x", {
         for(i in 1:3)
             cat("yippee!\n")
         10
     })

     x^2 #- yippee
     x^2 #- simple number

     e <- (function(x, y = 1, z) environment())(1+2, "y", {cat(" HO! "); pi+2})
     (le <- as.list(e)) # evaluates the promises

