delay                  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:

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

_U_s_a_g_e:

     delay(x, env = .GlobalEnv)

_A_r_g_u_m_e_n_t_s:

       x: an expression.

     env: an evaluation environment

_D_e_t_a_i_l_s:

     If promises are kept inside an 'environment' or 'list', they can
     be accessed in several ways without evaluation, see the examples
     below.

     When a promise is eventually forced, it is evaluated within the
     environment specified by 'env' (who contents may have changed in
     the meantime).

_V_a_l_u_e:

     A _promise_ to evaluate the expression.  The value which is
     returned by 'delay' can be assigned without forcing its
     evaluation, but any further accesses will cause evaluation.

_E_x_a_m_p_l_e_s:

     x <- delay({
         for(i in 1:7)
             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)) # a list with three promise components
     utils::str(le)     # even shows what's promised

     le$z # prints; does not eval
     ez <- le$z
     ez-2 # "HO!", pi
     ez # 5.14
     le$z # still promise

