Rev 68948 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/setLoadActions.Rd% Part of the R package, https://www.R-project.org% Copyright 2012-2014 R Core Team% Distributed under GPL 2 or later\name{setLoadActions}\alias{setLoadAction}\alias{setLoadActions}\alias{getLoadActions}\alias{hasLoadAction}\alias{evalOnLoad}\alias{evalqOnLoad}\title{Set Actions For Package Loading}\description{These functions provide a mechanism for packages to specifycomputations to be done during the loading of a package namespace.Such actions are a flexible way to provide information only available atload time (such as locations in a dynamically linked library).A call to \code{setLoadAction()} or \code{setLoadActions()} specifiesone or more functions to be called when the corresponding namespace isloaded, with the \dots{} argument names being used as identifyingnames for the actions.\code{getLoadActions} reports the currently defined load actions,given a package's namespace as its argument.\code{hasLoadAction} returns \code{TRUE} if a load actioncorresponding to the given name has previously been set for the\code{where} namespace.\code{evalOnLoad()} and \code{evalqOnLoad()} schedule a specificexpression for evaluation at load time.}\usage{setLoadAction(action, aname=, where=)setLoadActions(..., .where=)getLoadActions(where=)hasLoadAction(aname, where=)evalOnLoad(expr, where=, aname=)evalqOnLoad(expr, where=, aname=)}\arguments{\item{action, \dots}{functions of one or more arguments, to be called when this package isloaded. The functions will be called with one argument (the packagenamespace) so all following arguments must have default values.If the elements of \dots{} are named, these names will be used for thecorresponding load metadata.}\item{where, .where}{the namespace of the package for which the list of load actions aredefined. This argument is normally omitted if the call comes from thesource code for the package itself, but will be needed if a packagesupplies load actions for another package.}\item{aname}{the name for the action. If an action is set withoutsupplying a name, the default uses the position in the sequence ofactions specified (\code{".1"}, etc.).}\item{expr}{an expression to be evaluated in a load action inenvironment \code{where}. In the case of \code{evalqOnLoad()},the expression is interpreted literally, in that of\code{evalOnLoad()} it must be precomputed, typically as an objectof type \code{"language"}.}}\details{The \code{evalOnLoad()} and \code{evalqOnLoad()} functions are forconvenience. They construct a function to evaluate the expression andcall \code{setLoadAction()} to schedule a call to that function.Each of the functions supplied as an argument to \code{setLoadAction()}or \code{setLoadActions()} is saved as metadata in the namespace,typically that of the package containing the call to\code{setLoadActions()}. When this package's namespace is loaded, eachof these functions will be called. Action functions are called in theorder they are supplied to \code{setLoadActions()}. The objectsassigned have metadata names constructed from the names supplied in thecall; unnamed arguments are taken to be named by their position in thelist of actions (\code{".1"}, etc.).Multiple calls to \code{setLoadAction()} or \code{setLoadActions()}can be used in a package's code; the actions will be scheduled after anypreviously specified, except if the name given to \code{setLoadAction()}is that of an existing action. In typical applications,\code{setLoadActions()} is more convenient when calling from thepackage's own code to set several actions. Calls to\code{setLoadAction()} are more convenient if the action name is to beconstructed, which is more typical when one package constructs loadactions for another package.Actions can be revised by assigning with the same name, actual orconstructed, in a subsequent call. The replacement must still be avalid function, but can of course do nothing if the intention was toremove a previously specified action.The functions must have at least one argument. They will be called withone argument, the namespace of the package. The functions will becalled at the end of processing of S4 metadata, after dynamicallylinking any compiled code, the call to \code{.onLoad()}, if any, andcaching method and class definitions, but before the namespace issealed. (Load actions are only called if methods dispatch is on.)Functions may therefore assign or modify objects in the namespacesupplied as the argument in the call. The mechanism allows packagesto save information not available until load time, such as valuesobtained from a dynamically linked library.Load actions should be contrasted with user load hooks supplied by\code{\link{setHook}()}. User hooks are generally provided fromoutside the package and are run after the namespace has been sealed.Load actions are normally part of the package code, and the list ofactions is normally established when the package is installed.Load actions can be supplied directly in the source code for apackage. It is also possible and useful to provide facilities in onepackage to create load actions in another package. The software needsto be careful to assign the action functions in the correctenvironment, namely the namespace of the target package.}\value{\code{setLoadAction()} and \code{setLoadActions()} are called fortheir side effect and return no useful value.\code{getLoadActions()} returns a named list of the actions in thesupplied namespace.\code{hasLoadAction()} returns \code{TRUE} if the specified actionname appears in the actions for this package.}\seealso{\code{\link{setHook}} for safer (since they are run after thenamespace is sealed) and more comprehensive versions in thebase package.}\examples{\dontrun{## in the code for some package## ... somewhere elsesetLoadActions(function(ns)cat("Loaded package", sQuote(getNamespaceName(ns)),"at", format(Sys.time()), "\n"),setCount = function(ns) assign("myCount", 1, envir = ns),function(ns) assign("myPointer", getMyExternalPointer(), envir = ns))... somewhere laterif(countShouldBe0)setLoadAction(function(ns) assign("myCount", 0, envir = ns), "setCount")}}\keyword{ package }