Call hook functions by hook name.

# S3 method for default
callHooks(hookName, ..., removeCalledHooks=FALSE)

Arguments

hookName

A character string of the hook name.

...

Argument passed to each hook function.

removeCalledHooks

If TRUE, called hook functions are removed, otherwise not.

Value

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

Author

Henrik Bengtsson

See also

Internally, after retrieving hook functions, callHooks.list() is called.

Examples

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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()
#> Chair: Welcome to our conference.
#> Chair: Please welcome Speaker A!
#> Speaker A: Hello there...
#> Chair: Please thanks Speaker A!


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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)
#> List of 4
#>  $       :List of 3
#>   ..$ fcn      :function (n, mean = 0, sd = 1)  
#>   ..$ result   : num 0.351
#>   ..$ exception: NULL
#>  $ rexp  :List of 3
#>   ..$ fcn      : chr "rexp"
#>   ..$ result   : num 1.14
#>   ..$ exception: NULL
#>  $ runiff:List of 3
#>   ..$ fcn      : logi NA
#>   ..$ result   : NULL
#>   ..$ exception:List of 2
#>   .. ..$ message: chr "could not find function \"fcn\""
#>   .. ..$ call   : language fcn(...)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  $       :List of 3
#>   ..$ fcn      :<environment: R_GlobalEnv> 
#>   ..$ result   : NULL
#>   ..$ exception:List of 2
#>   .. ..$ message: chr "could not find function \"fcn\""
#>   .. ..$ call   : language fcn(...)
#>   .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
#>  - attr(*, "failedHooks")= logi [1:4] FALSE FALSE TRUE TRUE
cat("Number of hooks: ", length(res), "\n")
#> Number of hooks:  4 
isErroneous <- unlist(lapply(res, FUN=function(x) !is.null(x$exception)))
cat("Erroneous hooks: ", sum(isErroneous), "\n")
#> Erroneous hooks:  2