The R Project SVN R

Rev

Rev 60325 | Rev 66183 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60325 ripley 1
% File src/library/methods/man/setLoadActions.Rd
2
% Part of the R package, http://www.R-project.org
3
% Copyright 2012 R Core Team
4
% Distributed under GPL 2 or later
5
 
58639 jmc 6
\name{setLoadActions}
58656 jmc 7
\alias{setLoadAction}
58639 jmc 8
\alias{setLoadActions}
9
\alias{getLoadActions}
10
\alias{hasLoadAction}
58727 jmc 11
\alias{evalOnLoad}
12
\alias{evalqOnLoad}
58639 jmc 13
\title{
14
Set Actions For Package Loading
15
}
16
\description{
61433 ripley 17
These functions
58639 jmc 18
provide a mechanism
19
for packages to specify computations to be done during the loading of
20
the package namespace.
21
Such actions are a flexible way to provide information only available
22
at load time (such as locations in a dynamically linked library).
23
 
24
 
25
  A call to
58656 jmc 26
\code{setLoadAction()}  or \code{setLoadActions()} specifies one or more functions
58639 jmc 27
to be
28
called when the corresponding package is loaded, with the \dots{} argument
29
names being used as identifying names for the actions.
30
 
31
\code{getLoadActions} reports the currently defined load actions,
32
given a package's namespace as its argument.
33
 
34
\code{hasLoadAction} returns \code{TRUE} if a load action
35
corresponding to the given name has previously been set for the
36
\code{where} namespace.
58727 jmc 37
 
38
\code{evalOnLoad()} and \code{evalqOnLoad()} schedule a specific expression for
39
evaluation at load time.
58639 jmc 40
}
41
\usage{
42
 
58727 jmc 43
setLoadAction(action, aname=, where=)
58656 jmc 44
 
58639 jmc 45
setLoadActions(..., .where=)
46
 
47
getLoadActions(where=)
48
 
49
hasLoadAction(aname, where=)
58727 jmc 50
 
51
evalOnLoad(expr, where=, aname=)
52
 
53
evalqOnLoad(expr, where=, aname=)
54
 
58639 jmc 55
}
56
\arguments{
57
 
58656 jmc 58
  \item{action, \dots}{
58639 jmc 59
functions of one or more arguments, to be called when this package is
60
loaded. The functions will be called with one argument (the package
61
namespace) so all following arguments must have default values.
62
 
63
If the elements of \dots{} are named, these names will be used for the
64
corresponding load metadata.
65
}
66
 
67
  \item{where, .where}{
68
the namespace of the package for which the list of load actions are
69
defined. This argument is
70
normally omitted if the call comes from the source code for the package itself, but will
71
be needed if a package supplies load actions for another package.
72
}
73
 
58656 jmc 74
\item{aname}{the name for the action.  If an action is set without
75
    supplying a name,  the
76
    default uses the position in the sequence of actions specified
77
    (\code{".1"}, etc.).
58639 jmc 78
}
58727 jmc 79
 
80
\item{expr}{an expression to be evaluated in a load action in
81
    environment \code{where}.  In the case of \code{evalqOnLoad()},
82
    the expression is interpreted literally, in that of
83
    \code{evalOnLoad()} it must be precomputed, typically as an object
84
    of type \code{"language"}.
58639 jmc 85
}
58727 jmc 86
}
58639 jmc 87
\details{
58727 jmc 88
The \code{evalOnLoad()} and \code{evalqOnLoad()} functions are for
89
convenience.
90
They construct a function to evaluate the expression and call
91
\code{setLoadAction()} to schedule a call to that function.
92
 
58656 jmc 93
Each of the functions supplied as an argument to \code{setLoadAction()} or
94
\code{setLoadActions()} is saved as metadata in the
58639 jmc 95
namespace, typically that of the package containing the call to
96
\code{setLoadActions()}.
97
When this package's namespace is loaded, each of these functions will
98
be called.
99
Action functions are called in the order they are supplied to
100
\code{setLoadActions()}.
101
The objects assigned have metadata names constructed from the names
102
supplied in the call; unnamed arguments are taken to be named by their
103
position in the list of actions (\code{".1"}, etc.).
104
 
58656 jmc 105
Multiple calls to \code{setLoadAction()} or  \code{setLoadActions()} can be used in a package's
58639 jmc 106
code; the actions will be scheduled after any previously specified,
58656 jmc 107
except if the name given to \code{setLoadAction()} is that of an
108
existing action.
109
In typical applications, \code{setLoadActions()} is more convenient
110
when calling from the package's own code to set several actions.
111
Calls to \code{setLoadAction()} are more convenient if the action name
112
is to be constructed, which is more typical when one package
113
constructs load actions for another package.
114
 
58639 jmc 115
Actions can be revised by assigning with the same name, actual or
58656 jmc 116
constructed, in a subsequent call.
58639 jmc 117
The replacement must still be a valid function, but can of course do
118
nothing if the intention was to remove a previously specified action.
119
 
120
The functions must have at least one argument.  They will be called
121
with one argument, the namespace of the package.
122
The functions will be called at the end of processing of S4 metadata, after
123
dynamically linking any libraries, the call to \code{.onLoad()}, if
124
any, and caching method and class definitions, but before the namespace is sealed.
125
 
126
Functions may therefore assign or modify objects in the namespace
127
supplied as the argument in the call.
128
The mechanism allows packages to save information not available until
129
load time, such as values obtained from a dynamically linked library.
130
 
131
Load actions should be contrasted with user load hooks supplied by
132
\code{\link{setHook}()}.
133
User hooks are generally provided from outside the package and are run
134
after the namespace has been sealed.
135
Load actions are part of the package code, and the list of actions is
136
normally established when the package is installed.
137
 
138
 
58656 jmc 139
Load actions can be supplied directly in the source code for a package.
58639 jmc 140
It is also possible and useful to provide facilities in one package to
141
create load actions in another package.
142
The software needs to be careful to assign the action
143
functions in the correct environment, namely the namespace of the target
144
package.
145
 
146
}
147
\value{
58656 jmc 148
\code{setLoadAction()}  and \code{setLoadActions()} are called for their side effect and return no
58639 jmc 149
useful value.
150
 
151
\code{getLoadActions()} returns a named list of the actions in the
152
supplied namespace.
153
 
154
\code{hasLoadAction()} returns \code{TRUE} if the specified action
155
name appears in the actions for this package.
156
}
157
\examples{
158
\dontrun{
159
## in the code for some package
160
 
161
## ... somewhere else
162
setLoadActions(function(attach)
163
   cat(c("Loaded", "Unloaded")[attach], "at", Sys.time(), "\n"),
164
  setCount = function(ns) assign("myCount", 1, envir = ns),
165
  function(ns) assign("myPointer", getMyExternalPointer(), envir = ns))
166
  ... somewhere later
167
if(countShouldBe0)
58656 jmc 168
  setLoadAction(function(ns) assign("myCount", 0, envir = ns), "setCount")
58639 jmc 169
}
170
}
171
\keyword{ package }
172