The R Project SVN R

Rev

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