Rev 28110 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{ns-hooks}\alias{.onLoad}\alias{.onUnload}\alias{.onAttach}\title{Hooks for Name Space events}\description{Packages with name spaces can supply functions to be called whenloaded, attached or unloaded.}\usage{.onLoad(libname, pkgname).onAttach(libname, pkgname).onUnload(libpath)}\arguments{\item{libname}{a character string giving the library directory wherethe package defining the namespace was found.}\item{pkgname}{a character string giving the name of the name space.}\item{libpath}{a character string giving the complete path to the package.}}\details{These functions apply only to packages with name spaces.After loading, \code{\link{loadNamespace}} looks for a hook function named\code{.onLoad} and runs it before sealing the namespace and processingexports.If a name space is unloaded (via \code{\link{unloadNamespace}}),a hook function \code{.onUnload} is run before final unloading.Note that the code in \code{.onLoad} and \code{.onUnload} is runwithout the package being on the search path, but (unless circumvented)lexical scope will make objects in the namespace and its importsvisible. (Do not use the double colon operator in \code{.onLoad} asexports have not been processed at the point it is run.)When the package is attached (via \code{\link{library}}), the hookfunction \code{.onAttach} is called after the exported functions areattached. This is less likely to be useful than \code{.onLoad}, whichshould be seen as the analogue of \code{\link{.First.lib}} (which is onlyused for packages without a name space).\code{.onLoad}, \code{.onUnload} and \code{.onAttach} are looked foras internal variables in the name space and should not be exported.If a function \code{\link{.Last.lib}} is visible in the package, itwill be called when the package is detached: this does need to be exported.Anything needed for the functioning of the name space should behandled at load/unload times by the \code{.onLoad} and\code{.onUnload} hooks. For example, shared libraries can be loaded(unless done by a \code{useDynib} directive in the \code{NAMESPACE}file) and initialized in \code{.onLoad} and unloaded in\code{.onUnload}. Use \code{.onAttach} only for actions that areneeded only when the package becomes visible to the user, for examplea start-up message.}\seealso{\code{\link{setHook}} shows how users can set hooks on the same events.}\keyword{utilities}