callHooks              package:R.utils              R Documentation

_C_a_l_l _h_o_o_k _f_u_n_c_t_i_o_n_s _b_y _h_o_o_k _n_a_m_e

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

     Call hook functions by hook name.

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

     ## Default S3 method:
     callHooks(hookName, ..., removeCalledHooks=FALSE)

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

hookName: A 'character' string of the hook name.

     ...: Argument passed to each hook function.

removeCalledHooks: If 'TRUE', called hook functions are removed,
          otherwise not.

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

     Returns (invisibly) whatever 'callHooks.list'() returns.

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

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

     Internally, after retriving hook functions, 'callHooks.list'() is
     called. See UserHooks how to set hooks.

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

     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # Example 1
     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # First, clean up if called more than once
     setHook("myFunction.onEnter", NULL, action="replace")
     setHook("myFunction.onExit", NULL, action="replace")

     runConference <- function(...) {
       callHooks("myFunction.onEnter")
       cat("Speaker A: Hello there...\n")
       callHooks("myFunction.onExit")
     }

     setHook("myFunction.onEnter", function(...) {
       cat("Chair: Welcome to our conference.\n")
     })

     setHook("myFunction.onEnter", function(...) {
       cat("Chair: Please welcome Speaker A!\n")
     })

     setHook("myFunction.onExit", function(...) {
       cat("Chair: Please thanks Speaker A!\n")
     })

     runConference()

     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # Example 2
     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     setHook("randomNumber", NULL, action="replace")
     setHook("randomNumber", rnorm)      # By function
     setHook("randomNumber", "rexp")     # By name
     setHook("randomNumber", "runiff")   # Non-existing name
     setHook("randomNumber", .GlobalEnv) # Not a function

     res <- callHooks("randomNumber", n=1)
     str(res)
     cat("Number of hooks: ", length(res), "\n");
     isErroneous <- unlist(lapply(res, FUN=function(x) !is.null(x$exception)));
     cat("Erroneous hooks: ", sum(isErroneous), "\n");

