Rev 69484 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/userhooks.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2015 R Core Team% Distributed under GPL 2 or later\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 functions 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}\item{event}{character string: an event for the package. Can be abbreviated.}\item{value}{A function or a list of functions, or for \code{action = "replace"}, \code{NULL} }\item{action}{The action to be taken. Can be abbreviated.}}\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 was 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}} or loaded byother means, it can call initialization code. See\code{\link{.onLoad}} for a description of the package hook functionscalled during initialization. Users can add their own initializationcode via the hooks provided by \code{setHook()}, functions which willbe called as \code{funname(pkgname, pkgpath)} inside a\code{\link{try}} call.The sequence of events depends on which hooks are defined, and whethera package is attached or just loaded. In the case where all hooksare defined and a package is attached, the order of initializationevents is as follows:\enumerate{\item The package namespace is loaded.\item The package's \code{\link{.onLoad}} function is run.\item If S4 methods dispatch is on, any actions set by\code{\link{setLoadAction}} are run.\item The namespace is sealed.\item The user's \code{"onLoad"} hook is run.\item The package is added to the search path.\item The package's \code{\link{.onAttach}} function is run.\item The package environment is sealed.\item The user's \code{"attach"} hook is run.}A similar sequence (but in reverse) is run when a package is detachedand its namespace unloaded:\enumerate{\item The user's \code{"detach"} hook is run.\item The package's \code{\link{.Last.lib}} function is run.\item The package is removed from the search path.\item The user's \code{"onUnload"} hook is run.\item The package's \code{\link{.onUnload}} function is run.\item The package namespace is unloaded.}Note that when an \R session is finished, packages are not detached andnamespaces are not unloaded, so the corresponding hooks will not berun.Also note that some of the user hooks are run without the packagebeing on the search path, so in those hooks objects in the packageneed 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 shownby \code{getHook}, but the \code{"detach"} and \code{"onUnload"} hooksare run in reverse order so the default for package events is to addhooks \sQuote{inside} existing ones.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 (possibly empty).For \code{setHook} function, no return value.For \code{packageEvent}, the derived hook name (a character string).}\note{Hooks need to be set before the event they modify: for standardpackages this can be problematic as \pkg{methods} is loaded andattached early in the startup sequence. The usual place to set hookssuch as the example below is in the \file{.Rprofile} file, but thatwill not work for \pkg{methods}.}\seealso{\code{\link{library}}, \code{\link{detach}}, \code{\link{loadNamespace}}.See \code{\link{::}}for a discussion of the double and triple colon operators.Other hooks may be added later: functions \code{\link{plot.new}} and\code{\link{persp}} already have them.}\examples{setHook(packageEvent("grDevices", "onLoad"),function(...) grDevices::ps.options(horizontal = FALSE))}\keyword{utilities}