ID EN
Miscellaneous

userhooks

R Base 3.6.2

These functions allow users to set actions to be taken before packages are attached/detached and namespaces are (un)loaded.

Syntax

R
getHook(hookName)
setHook(hookName, value,
        action = c("append", "prepend", "replace"))<p></p><p>packageEvent(pkgname,
             event = c("onLoad", "attach", "detach", "onUnload"))</p>

Arguments

Parameter Description
hookName character string: the hook name
pkgname character string: the package/namespace name
event character string: an event for the package. Can be abbreviated.
value A function or a list of functions, or for action = "replace", NULL
action The action to be taken. Can be abbreviated.

Return Value

For getHook function, a list of functions (possibly empty). For setHook function, no return value. For packageEvent, the derived hook name (a character string).

Details

setHook provides a general mechanism for users to register hooks, a list of functions to be called from system (or user) functions. The initial set of hooks was associated with events on packages/namespaces: these hooks are named via calls to packageEvent. To remove a hook completely, call setHook(hookName, NULL, "replace"). When an R package is attached by library or loaded by other means, it can call initialization code. See .onLoad for a description of the package hook functions called during initialization. Users can add their own initialization code via the hooks provided by setHook(), functions which will be called as funname(pkgname, pkgpath) inside a try call. The sequence of events depends on which hooks are defined, and whether a package is attached or just loaded. In the case wh

Examples

Example
R
# NOT RUN {
setHook(packageEvent("grDevices", "onLoad"),
        function(...) grDevices::ps.options(horizontal = FALSE))
# }

See Also

library detach loadNamespace. See :: for a discussion of the double and triple colon operators. Other hooks may be added later: functions plot.new and persp already have them.