The R Project SVN R

Rev

Rev 68948 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60325 ripley 1
% File src/library/methods/man/evalSource.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
60325 ripley 3
% Copyright 2010-2 R Core Team
4
% Distributed under GPL 2 or later
5
 
52778 jmc 6
\name{evalSource}
56186 murdoch 7
\alias{evalSource}
52778 jmc 8
\alias{insertSource}
9
\alias{sourceEnvironment-class}
10
\title{
11
Use Function Definitions from a Source File without Reinstalling a Package
12
}
13
\description{
14
Definitions of functions and/or methods from a source file are
15
inserted into a package, using the \code{\link{trace}} mechanism.
16
Typically, this allows testing or debugging modified versions of a few
17
functions without reinstalling a large package.
18
}
19
\usage{
20
evalSource(source, package = "", lock = TRUE, cache = FALSE)
21
 
52791 jmc 22
insertSource(source, package = "", functions = , methods = ,
23
           force = )
52778 jmc 24
 
25
}
26
\arguments{
27
  \item{source}{
28
A file to be parsed and evaluated by \code{evalSource} to find the new
29
function and method definitions.
30
 
31
The argument to \code{insertSource} can be an object of class \code{"sourceEnvironment"}
32
returned from a previous call
33
to \code{evalSource}   If a file name is passed to \code{insertSource}
34
it calls \code{evalSource} to obtain the corresponding object.  See
35
the section on the class for details.
36
 
37
}
38
  \item{package}{
39
Optionally, the name of the package to which the new code corresponds
40
and into which it will be
41
inserted.  Although the computations will attempt to infer the package
42
if it is omitted, the safe approach is to supply it.  In the case of a
43
package that is not attached to the search list, the package name must
44
be supplied.
45
}
46
  \item{functions, methods}{
47
Optionally, the character-string names of the functions to be
48
used in the insertion.  Names supplied in the \code{functions}
49
argument are expected to be defined as functions in the source.
50
For names supplied in the \code{methods} argument, a table of methods
51
is expected (as generated by calls to \code{\link{setMethod}}, see the
52
details section); methods from this table will be inserted by
53
\code{insertSource}.  In both cases, the revised function or method is
54
inserted only if it differs from the version in the corresponding
55
package as loaded.
56
 
57
If \code{what} is omitted, the results of evaluating the source file
58
will be compared to the contents of the package (see the details section).
59
}
60
\item{lock, cache}{
61
Optional arguments to control the actions taken by \code{evalSource}.
62
If \code{lock} is \code{TRUE}, the environment in the object returned
63
will be locked, and so will all its bindings.
64
If \code{cache} is \code{FALSE}, the normal caching of method and
65
class definitions will be suppressed during evaluation of the
66
\code{source} file.
67
 
68
The default settings are generally recommended, the \code{lock} to
69
support the credibility of the object returned as a snapshot of the
70
source file, and the second so that method definitions can be inserted
71
later by \code{insertSource} using the trace mechanism.
72
}
52791 jmc 73
\item{force}{
74
If \code{FALSE}, only functions currently in the environment will be
75
redefined, using \code{\link{trace}}.  If \code{TRUE}, other
76
objects/functions will be simply assigned.  By default, \code{TRUE} if
77
neither the \code{functions} nor the \code{methods} argument is supplied.
78
}
52778 jmc 79
 
80
}
81
\details{
82
The \code{source} file is parsed and evaluated, suppressing by default
83
the actual caching of method and class definitions contained in it, so
84
that functions and methods can be tested out in a reversible way.
85
The result, if all goes well, is an environment containing the
86
assigned objects and metadata corresponding to method and class definitions
87
in the source file.
88
 
89
From this environment, the objects are inserted into the package, into
90
its namespace if it has one, for use during the current session or
91
until reverting to the original version by a call to
92
\code{\link{untrace}}.
93
The insertion is done by calls to the internal version of
94
\code{\link{trace}}, to make reversion possible.
95
 
96
Because the trace mechanism is used, only function-type objects will
97
be inserted, functions themselves or S4 methods.
98
 
99
When the \code{functions} and \code{methods} arguments are both
100
omitted, \code{insertSource} selects all suitable objects from the
101
result of evaluating the \code{source} file.
102
 
103
In all cases,
104
only objects in the source file that differ from
105
the corresponding objects in the package are inserted.
106
The definition of \dQuote{differ} is that either the argument list
107
(including default expressions) or the body of the function is not
108
identical.
109
Note that in the case of a method, there need be no specific method
110
for the corresponding signature in the package: the comparison is made
111
to the method that would be selected for that signature.
112
 
113
Nothing in the computation requires that the source file supplied be
114
the same file as in the original package source, although that case is
115
both likely and sensible if one is revising the package.  Nothing in
116
the computations compares source files:  the objects generated by
117
evaluating \code{source} are compared as objects to the content of the package.
118
}
119
\value{
120
An object from class \code{"sourceEnvironment"}, a subclass of
121
\code{"environment"} (see the section on the class)
122
The environment contains the versions
123
of \emph{all}  object resulting from evaluation of the source file.
124
The class also has slots for the time of creation, the source file
125
and the package name.
126
Future extensions may use these objects for versioning or other code tools.
127
 
128
 
129
The object returned can be used in debugging (see the section on that
130
topic) or as the \code{source}
131
argument in a future call to \code{insertSource}.  If only some of the
132
revised functions were inserted in the first call, others can be
133
inserted in a later call without re-evaluating the source file, by
134
supplying the environment and optionally suitable \code{functions}
135
and/or \code{methods} argument.
136
}
137
 
138
\section{Debugging}{
139
Once a function or method has been inserted into a package by
140
\code{insertSource}, it can be studied by the standard debugging tools;
141
for example, \code{\link{debug}} or the various versions of
142
\code{\link{trace}}.
143
 
144
Calls to \code{\link{trace}} should take the extra argument \code{edit
145
= env}, where \code{env} is the value returned by the call to
146
\code{evalSource}.
147
The trace mechanism has been used to install the revised version from
148
the source file, and supplying the argument ensures that it is this
149
version, not the original, that will be traced.  See the example
150
below.
151
 
152
To turn tracing off, but retain the source version, use \code{trace(x,
153
edit = env)} as in the example.  To return to the original version
154
from the package, use \code{untrace(x)}.
155
}
156
 
81927 smeyer 157
\section{Class \code{"sourceEnvironment"}}{
52778 jmc 158
Objects from this class can be treated as environments, to extract the
159
version of functions and methods generated by \code{evalSource}.
160
The objects also have the following slots:
161
\describe{
162
\item{\code{packageName}:}{ The character-string name of the package
163
    to which the source code corresponds.
164
}
165
\item{\code{dateCreated}:}{ The date and time that the source file was
166
    evaluated (usually from a call to \code{\link{Sys.time}}).
167
}
168
\item{\code{sourceFile}:}{ The character-string name of the source file
169
    used.
170
}
171
Note that using the environment does not change the \code{dateCreated}.
172
}
173
}
174
 
175
\seealso{
176
\code{\link{trace}} for the underlying mechanism, and also for the
177
\code{edit=} argument that can be used for somewhat similar purposes;
178
that function and also \code{\link{debug}} and
179
\code{\link{setBreakpoint}}, for techniques more oriented to
180
traditional debugging styles.
181
The present function is directly intended for the case that one is
182
modifying some of the source for an existing package, although it can
183
be used as well by inserting debugging code in the source (more useful
184
if the debugging involved is non-trivial).  As noted in the details
185
section, the source
186
file need not be the same one in the original package source.
187
}
188
\examples{
189
\dontrun{
190
## Suppose package P0 has a source file "all.R"
191
## First, evaluate the source, and from it
192
## insert the revised version of methods for summary()
193
  env <- insertSource("./P0/R/all.R", package = "P0",
194
     methods = "summary")
195
## now test one of the methods, tracing  the version from the source
196
  trace("summary", signature = "myMat", browser, edit = env)
197
## After testing, remove the browser() call but keep the source
198
  trace("summary", signature = "myMat", edit = env)
199
## Now insert all the (other) revised functions and methods
200
## without re-evaluating the source file.
201
## The package name is included in the object env.
202
  insertSource(env)
203
}
204
}
205
\keyword{ programming }
206
\keyword{methods }