Rev 28126 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{UserHooks}\alias{getHook}\alias{setHook}\alias{packageEvent}\alias{.userHooksEnv}\title{Functions to Get and Set Hooks for Load, Attach, Detach and Unload}\description{These function allow users to set actions to be taken before packagesare attached/detached and namespaces are (un)loaded.}\usage{getHook(hookName)setHook(hookName, value, action = c("append", "prepend", "replace"))packageEvent(pkgname, event=c("onLoad", "attach", "detach", "onUnload"))}\arguments{\item{hookName}{character string: the hook name}\item{pkgname}{character string: the package/namespace name.If versioned install has been used, \code{pkgname} should the unversionedname of the package and any version information will be stripped.}\item{event}{character string: an event for the package}\item{value}{A function, or for \code{action="replace"}, \code{NULL}.}\item{action}{The action to be taken. The names can be appreviated.}}\details{\code{setHook} provides a general mechanism for users to registerhooks, a list of functions to be called from system (or user)functions. The initial set of hooks is associated with events onpackages/namespaces: these hooks are named via calls to \code{packageEvent}.To remove a hook completely, call \code{setHook(hookName, NULL, "replace")}.When an \R package is attached by \code{\link{library}}, it can callinitialization code via a function \code{.First.lib}, and when it is\code{\link{detach}}-ed it can tidy up via a function \code{.Last.lib}.Users can add their own initialization code via the hooks provided bythese functions, functions which will be called as\code{funname(pkgname, pkgpath)} inside a \code{\link{try}}call. (The attach hook is called after \code{.First.lib} and the detachhook before \code{.Last.lib}.)If a package has a namespace, there are two further actions, when thenamespace is loaded (before being attached and after \code{.onLoad} iscalled ) and when it is unloaded (after being detached and before\code{.onUnload}). Note that code in these hooks is run without thepackage being on the search path, so objects in the package need to bereferred to using the double colon operator as in the example.(Unlike \code{.onLoad}, the user hook is run after the name space hasbeen sealed.)Hooks are normally run in the order shown by \code{getHook},but the \code{"detach"} and \code{"onUnload"} hooks are runin reverse order so the default for package events is to add hooks\sQuote{inside} existing ones.Note that when an \R is finished, packages are not detached andnamespaces are not unloaded, so the corresponding hooks will not berun.The hooks are stored in the environment \code{.userHooksEnv} in thebase package, with \sQuote{mangled} names.}\value{For \code{getHook} function, a list of functions (possible empty).For \code{setHook} function, no return value.For \code{packageEvent}, the derived hook name (a character string).}\seealso{\code{\link{library}}, \code{\link{detach}}, \code{\link{loadNamespace}}.Other hooks may be added later: \code{\link[graphics]{plot.new}} and\code{\link[graphics]{persp}} already have them.}\examples{setHook(packageEvent("graphics", "onLoad"),function(...) graphics::ps.options(horizontal=FALSE))}\keyword{utilities}