callHooks

0th

Percentile

Call hook functions by hook name

Call hook functions by hook name.

Keywords
programming
Usage
## S3 method for class '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

See Also

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

Aliases
  • callHooks.default
  • callHooks
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...
")
  callHooks("myFunction.onExit")
}

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

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

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

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), "");
isErroneous <- unlist(lapply(res, FUN=function(x) !is.null(x$exception)));
cat("Erroneous hooks: ", sum(isErroneous), "");
Documentation reproduced from package R.utils, version 1.7.3, License: LGPL (>= 2.1)

Community examples

Looks like there are no examples yet.