ID EN
Miscellaneous

ns-hooks

R Base 3.6.2

Packages can supply functions to be called when loaded, attached, detached or unloaded.

Syntax

R
.onLoad(libname, pkgname)
.onAttach(libname, pkgname)
.onUnload(libpath)
.onDetach(libpath)
.Last.lib(libpath)

Arguments

Parameter Description
libname a character string giving the library directory where the package defining the namespace was found.
pkgname a character string giving the name of the package.
libpath a character string giving the complete path to the package.

Details

After loading, loadNamespace looks for a hook function named .onLoad and calls it (with two unnamed arguments) before sealing the namespace and processing exports. When the package is attached (via library or attachNamespace), the hook function .onAttach is looked for and if found is called (with two unnamed arguments) before the package environment is sealed. If a function .onDetach is in the namespace or .Last.lib is exported from the package, it will be called (with a single argument) when the package is detached. Beware that it might be called if .onAttach has failed, so it should be written defensively. (It is called within tryCatch, so errors will not stop the package being detached.) If a namespace is unloaded (via unloadNamespace), a hook function .onUnload is run (with a single ar

See Also

setHook shows how users can set hooks on the same events and lists the sequence of events involving all of the hooks. reg.finalizer for hooks to be run at the end of a session. loadNamespace for more about namespaces.