Functions to Get and Set Hooks for Load, Attach, Detach and Unload


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


setHook(hookName, value,
        action = c("append", "prepend", "replace"))

             event = c("onLoad", "attach", "detach", "onUnload"))



character string: the hook name


character string: the package/namespace name


character string: an event for the package. Can be abbreviated.


A function or a list of functions, or for action = "replace", NULL


The action to be taken. Can be abbreviated.


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 where all hooks are defined and a package is attached, the order of initialization events is as follows:

  1. The package namespace is loaded.

  2. The package's .onLoad function is run.

  3. If S4 methods dispatch is on, any actions set by setLoadAction are run.

  4. The namespace is sealed.

  5. The user's "onLoad" hook is run.

  6. The package is added to the search path.

  7. The package's .onAttach function is run.

  8. The package environment is sealed.

  9. The user's "attach" hook is run.

A similar sequence (but in reverse) is run when a package is detached and its namespace unloaded:

  1. The user's "detach" hook is run.

  2. The package's .Last.lib function is run.

  3. The package is removed from the search path.

  4. The user's "onUnload" hook is run.

  5. The package's .onUnload function is run.

  6. The package namespace is unloaded.

Note that when an R session is finished, packages are not detached and namespaces are not unloaded, so the corresponding hooks will not be run.

Also note that some of the user hooks are run without the package being on the search path, so in those hooks objects in the package need to be referred to using the double (or triple) colon operator, as in the example.

If multiple hooks are added, they are normally run in the order shown by getHook, but the "detach" and "onUnload" hooks are run in reverse order so the default for package events is to add hooks ‘inside’ existing ones.

The hooks are stored in the environment .userHooksEnv in the base package, with ‘mangled’ names.


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


Hooks need to be set before the event they modify: for standard packages this can be problematic as methods is loaded and attached early in the startup sequence. The usual place to set hooks such as the example below is in the ‘.Rprofile’ file, but that will not work for methods.

See Also

library, detach, loadNamespace.

See :: for a discussion of the double and triple colon operators.

Other hooks may be added later: functions and persp already have them.


setHook(packageEvent("grDevices", "onLoad"),
        function(...) grDevices::ps.options(horizontal = FALSE))

Want to suggest features or report bugs for Use the GitHub issue tracker.

comments powered by Disqus