Rev 69065 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/ns-hooks.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2017 R Core Team% Distributed under GPL 2 or later\name{ns-hooks}\alias{.onLoad}\alias{.onUnload}\alias{.onAttach}\alias{.onDetach}\alias{.Last.lib}\title{Hooks for Namespace Events}\description{Packages can supply functions to be called whenloaded, attached, detached or unloaded.}\usage{.onLoad(libname, pkgname).onAttach(libname, pkgname).onUnload(libpath).onDetach(libpath).Last.lib(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 package.}\item{libpath}{a character string giving the complete path to the package.}}\details{After loading, \code{\link{loadNamespace}} looks for a hook functionnamed \code{.onLoad} and calls it (with two unnamed arguments) beforesealing the namespace and processing exports.When the package is attached (via \code{\link{library}} or\code{\link{attachNamespace}}), the hook function \code{.onAttach} islooked for and if found is called (with two unnamed arguments) beforethe package environment is sealed.If a function \code{.onDetach} is in the namespace or \code{.Last.lib}is exported from the package, it will be called (with a singleargument) when the package is \code{\link{detach}}ed. Beware that itmight be called if \code{.onAttach} has failed, so it should bewritten defensively. (It is called within \code{\link{tryCatch}}, soerrors will not stop the package being detached.)If a namespace is unloaded (via \code{\link{unloadNamespace}}), a hookfunction \code{.onUnload} is run (with a single argument) before finalunloading.Note that the code in \code{.onLoad} and \code{.onUnload} should notassume any package except the base package is on the search path.Objects in the current package will be visible (unless this iscircumvented), but objects from other packages should be imported orthe double colon operator should be used.\code{.onLoad}, \code{.onUnload}, \code{.onAttach} and\code{.onDetach} are looked for as internal objects in the namespaceand should not be exported (whereas \code{.Last.lib} should be).Note that packages are not detached nor namespaces unloaded at the endof an \R session unless the user arranges to do so (e.g., \emph{via}\code{\link{.Last}}).Anything needed for the functioning of the namespace should behandled at load/unload times by the \code{.onLoad} and\code{.onUnload} hooks. For example, DLLs can be loaded (unless doneby a \code{useDynLib} directive in the \file{NAMESPACE} file) andinitialized in \code{.onLoad} and unloaded in \code{.onUnload}. Use\code{.onAttach} only for actions that are needed only when thepackage becomes visible to the user (for example a start-up message)or need to be run after the package environment has been created.}\section{Good practice}{Loading a namespace should where possible be silent, with startupmessages given by \code{.onAttach}. These messages (and any essentialones from \code{.onLoad}) should use \code{\link{packageStartupMessage}}so they can be silenced where they would be a distraction.There should be no calls to \code{library} nor \code{require} in thesehooks. The way for a package to load other packages is via the\samp{Depends} field in the \file{DESCRIPTION} file: this ensuresthat the dependence is documented and packages are loaded in thecorrect order. Loading a namespace should not change the search path,so rather than attach a package, dependence of a namespace on anotherpackage should be achieved by (selectively) importing from the otherpackage's namespace.Uses of \code{library} with argument \code{help} to display basicinformation about the package should use \code{format} on thecomputed package information object and pass this to\code{packageStartupMessage}.There should be no calls to \code{\link{installed.packages}} in startupcode: it is potentially very slow and may fail in versions of \Rbefore 2.14.2 if package installation is going on in parallel. Seeits help page for alternatives.Compiled code should be loaded (e.g., \emph{via}\code{\link{library.dynam}}) in \code{.onLoad} or a \code{useDynLib}directive in the \file{NAMESPACE} file, and not in \code{.onAttach}.Similarly, compiled code should not be unloaded (e.g., \emph{via}\code{\link{library.dynam.unload}}) in \code{.Last.lib} nor\code{.onDetach}, only in \code{.onUnload}.}\seealso{\code{\link{setHook}} shows how users can set hooks on the same events, andlists the sequence of events involving all of the hooks.\code{\link{reg.finalizer}} for hooks to be run at the end of a session.\code{\link{loadNamespace}} for more about namespaces.}\keyword{utilities}