dwatch {debugr} | R Documentation |
Printing debug outputs during runtime
Description
Prints a debug output to the console or to a file. A debug output can consist
of a static text message, the values of one or more objects (potentially
transformed by applying some functions) or the value of one or multiple (more
complex) R expressions. Whether or not a debug message is displayed can be
made dependent on the evaluation of a criterion phrased as an R expression.
Generally, debug messages are only shown if the debug mode is activated. The
debug mode is activated and deactivated with debugr_switchOn
and debugr_switchOff
, respectively, which change the logical
debugr.active
value in the global options. Since debug messages are
only displayed in debug mode, the dwatch
function calls can even
remain in the original code as they remain silent and won't have any effect
until the debug mode is switched on again.
Usage
dwatch(crit = "", objs = NULL, funs = NULL, args = NULL,
show.all = FALSE, expr = NULL, msg = "", halt = FALSE,
unique.id = "", suppress.source = FALSE, show.frame = TRUE,
filename = "")
Arguments
crit |
An string containing an expression that determines if any debug
outputs shall be displayed at all. Only, if |
objs |
A vector of object names (as strings). The values of these objects will be displayed in the debug output. |
funs |
A vector of function names (as strings) that shall be applied to
the objects in |
args |
A list of vectors containing additional arguments for the
functions in |
show.all |
Prints all objects from the (calling) environment. If set to
|
expr |
A vector of strings containing expressions to be evaluated and
displayed in the debug output. This output comes on top of any |
msg |
A string containing a general message to be displayed. |
halt |
If |
unique.id |
A unqiue string ID that can be chosen by the user. This ID
is displayed in the debug output and is used to identify the code section
that contains the |
suppress.source |
If |
show.frame |
If |
filename |
If a filename is provided, all debug message are only printed to the file and not shown on the R console. |
See Also
debugr_switchOn, debugr_switchOff, debugr_isActive
Examples
library(debugr)
# --- A simple example to print the value of an object
myfunction <- function(x) {
justastring <- "Not much information here"
z <- 1
for(i in 1:x) {
# This call can remain in your code; it is only activated when
# the debug mode is switched on
dwatch(crit = "z > 40000", objs = c("z"))
z <- z * i
}
invisible(z)
}
# Turn debug mode on
debugr_switchOn()
# Call function for debugging
myfunction(10)
# --- Applying a function to the object that is printed
myfunction <- function(x) {
justastring <- "Not much information here"
z <- 1
for(i in 1:x) {
dwatch(crit = "z > 40000", objs = c("z"), funs=c("format"),
args = as.list(c(big.mark = "\",\"")))
z <- z * i
}
invisible(z)
}
myfunction(10)
# --- Same thing, this time with a expression
myfunction <- function(x) {
justastring <- "Not much information here"
z <- 1
for(i in 1:x) {
dwatch(crit = "z > 40000", expr=c("format(z, big.mark = \",\")"))
z <- z * i
}
invisible(z)
}
myfunction(10)