trace
mechanism.
Typically, this allows testing or debugging modified versions of a few
functions without reinstalling a large package.evalSource(source, package = "", lock = TRUE, cache = FALSE)insertSource(source, package = "", functions = , methods = ,
force = )
evalSource
to find the new
function and method definitions.The argument to insertSource
can be an object of class "sourceEnvironment"
returned from a previous call
to evalSource
If a file name is passed to insertSource
it calls evalSource
to obtain the corresponding object. See
the section on the class for details.
functions
argument are expected to be defined as functions in the source.
For names supplied in the methods
argument, a table of methods
is expected (as generated by calls to setMethod
, see the
details section); methods from this table will be inserted by
insertSource
. In both cases, the revised function or method is
inserted only if it differs from the version in the corresponding
package as loaded.If what
is omitted, the results of evaluating the source file
will be compared to the contents of the package (see the details section).
evalSource
.
If lock
is TRUE
, the environment in the object returned
will be locked, and so will all its bindings.
If cache
is FALSE
, the normal caching of method and
class definitions will be suppressed during evaluation of the
source
file.The default settings are generally recommended, the lock
to
support the credibility of the object returned as a snapshot of the
source file, and the second so that method definitions can be inserted
later by insertSource
using the trace mechanism.
FALSE
, only functions currently in the environment will be
redefined, using trace
. If TRUE
, other
objects/functions will be simply assigned. By default, TRUE
if
neither the functions
nor the methods
argument is supplied."sourceEnvironment"
, a subclass of
"environment"
(see the section on the class)
The environment contains the versions
of all object resulting from evaluation of the source file.
The class also has slots for the time of creation, the source file
and the package name.
Future extensions may use these objects for versioning or other code tools.
The object returned can be used in debugging (see the section on that
topic) or as the source
argument in a future call to insertSource
. If only some of the
revised functions were inserted in the first call, others can be
inserted in a later call without re-evaluating the source file, by
supplying the environment and optionally suitable functions
and/or methods
argument.
insertSource
, it can be studied by the standard debugging tools;
for example, debug
or the various versions of
trace
.Calls to trace
should take the extra argument edit
= env
, where env
is the value returned by the call to
evalSource
.
The trace mechanism has been used to install the revised version from
the source file, and supplying the argument ensures that it is this
version, not the original, that will be traced. See the example
below.
To turn tracing off, but retain the source version, use trace(x,
edit = env)
as in the example. To return to the original version
from the package, use untrace(x)
.
source
file is parsed and evaluated, suppressing by default
the actual caching of method and class definitions contained in it, so
that functions and methods can be tested out in a reversible way.
The result, if all goes well, is an environment containing the
assigned objects and metadata corresponding to method and class definitions
in the source file.From this environment, the objects are inserted into the package, into
its namespace if it has one, for use during the current session or
until reverting to the original version by a call to
untrace
.
The insertion is done by calls to the internal version of
trace
, to make reversion possible.
Because the trace mechanism is used, only function-type objects will be inserted, functions themselves or S4 methods.
When the functions
and methods
arguments are both
omitted, insertSource
selects all suitable objects from the
result of evaluating the source
file.
In all cases,
only objects in the source file that differ from
the corresponding objects in the package are inserted.
The definition of
Nothing in the computation requires that the source file supplied be
the same file as in the original package source, although that case is
both likely and sensible if one is revising the package. Nothing in
the computations compares source files: the objects generated by
evaluating source
are compared as objects to the content of the package.
trace
for the underlying mechanism, and also for the
edit=
argument that can be used for somewhat similar purposes;
that function and also debug
and
setBreakpoint
, for techniques more oriented to
traditional debugging styles.
The present function is directly intended for the case that one is
modifying some of the source for an existing package, although it can
be used as well by inserting debugging code in the source (more useful
if the debugging involved is non-trivial). As noted in the details
section, the source
file need not be the same one in the original package source.## Suppose package P0 has a source file "all.R"
## First, evaluate the source, and from it
## insert the revised version of methods for summary()
env <- insertSource("./P0/R/all.R", package = "P0",
methods = "summary")
## now test one of the methods, tracing the version from the source
trace("summary", signature = "myMat", browser, edit = env)
## After testing, remove the browser() call but keep the source
trace("summary", signature = "myMat", edit = env)
## Now insert all the (other) revised functions and methods
## without re-evaluating the source file.
## The package name is included in the object env.
insertSource(env)
Run the code above in your browser using DataLab