keep.source = TRUE
.
findLineNum(srcfile, line, nameonly = TRUE, envir = parent.frame(), lastenv)
setBreakpoint(srcfile, line, nameonly = TRUE, envir = parent.frame(), lastenv, verbose = TRUE, tracer, print = FALSE, clear = FALSE, ...)
TRUE
(the default), we require only a match
to basename(srcfile)
, not to the full path.print
argument to pass to trace
.trace
.findLineNum
function searches through all objects in
environment envir
, its parent, grandparent, etc., all the way
back to lastenv
. lastenv
defaults to the global environment if
envir
is not specified, and to the
root environment emptyenv()
if envir
is
specified. (The first default tends to be quite fast, and will
usually find all user code other than S4 methods; the second one is
quite slow, as it will typically search all attached system
libraries.)
For convenience, envir
may be specified indirectly: if it is
not an environment, it will be replaced with
environment(envir)
.
setBreakpoint
is a simple wrapper function for
trace
and untrace
. It will set or clear
breakpoints at the locations found by findLineNum
.
The srcfile
is normally a filename entered as a character
string, but it may be a "srcfile"
object, or it may
include a suffix like "filename.R#nn"
, in which case the number
nn
will be used as a default value for line
.
As described in the description of the where
argument on the
man page for trace
, the R package system uses a
complicated scheme that may include more than one copy of a function
in a package. The user will typically see the public one on the
search path, while code in the package will see a private one in the
package namespace. If you set envir
to the environment of a
function in the package, by default findLineNum
will find both
versions, and setBreakpoint
will set the breakpoint in both.
(This can be controlled using lastenv
; e.g.,
envir = environment(foo)
, lastenv = globalenv()
will find only the private copy, as the search is stopped before
seeing the public copy.)
S version 4 methods are also somewhat tricky to find. They are stored
with the generic function, which may be in the base or other
package, so it is usually necessary to have lastenv = emptyenv()
in order to find them. In some cases transformations are done by R
when storing them and findLineNum
may not be able to find the
original code. Many special cases, e.g.\ifelse{latex}{\out{~}}{ } methods on primitive
generics, are not yet supported.
trace
## Not run:
# # Find what function was defined in the file mysource.R at line 100:
# findLineNum("mysource.R#100")
#
# # Set a breakpoint in both copies of that function, assuming one is in the
# # same namespace as myfunction and the other is on the search path
# setBreakpoint("mysource.R#100", envir = myfunction)
# ## End(Not run)
Run the code above in your browser using DataLab