Use Function Definitions from a Source File without Reinstalling a Package
Definitions of functions and/or methods from a source file are
inserted into a package, using the
Typically, this allows testing or debugging modified versions of a few
functions without reinstalling a large package.
1 2 3 4
A file to be parsed and evaluated by
The argument to
Optionally, the name of the package to which the new code corresponds and into which it will be inserted. Although the computations will attempt to infer the package if it is omitted, the safe approach is to supply it. In the case of a package that is not attached to the search list, the package name must be supplied.
Optionally, the character-string names of the functions to be
used in the insertion. Names supplied in the
Optional arguments to control the actions taken by
The default settings are generally recommended, the
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
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.
methods arguments are both
insertSource selects all suitable objects from the
result of evaluating the
In all cases, only objects in the source file that differ from the corresponding objects in the package are inserted. The definition of “differ” is that either the argument list (including default expressions) or the body of the function is not identical. Note that in the case of a method, there need be no specific method for the corresponding signature in the package: the comparison is made to the method that would be selected for that signature.
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
source are compared as objects to the content of the package.
An object from class
"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
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
Once a function or method has been inserted into a package by
insertSource, it can be studied by the standard debugging tools;
debug or the various versions of
trace should take the extra argument
= env, where
env is the value returned by the call to
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
To turn tracing off, but retain the source version, use
edit = env) as in the example. To return to the original version
from the package, use
Objects from this class can be treated as environments, to extract the
version of functions and methods generated by
The objects also have the following slots:
The character-string name of the package to which the source code corresponds.
The date and time that the source file was evaluated (usually from a call to
The character-string name of the source file used.
Note that using the environment does not change the
trace for the underlying mechanism, and also for the
edit= argument that can be used for somewhat similar purposes;
that function and also
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## Not run: ## 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) ## End(Not run)
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.