# NOT RUN {
require(stats)
## Very simple use
trace(sum)
hist(rnorm(100)) # shows about 3-4 calls to sum()
untrace(sum)
## Show how pt() is called from inside power.t.test():
if(FALSE)
trace(pt) ## would show ~20 calls, but we want to see more:
trace(pt, tracer = quote(cat(sprintf("tracing pt(*, ncp = %.15g)\n", ncp))),
print = FALSE) # <- not showing typical extra
power.t.test(20, 1, power=0.8, sd=NULL) ##--> showing the ncp root finding:
untrace(pt)
# }
# NOT RUN {
<!-- %% methods is loaded when needed -->
# }
# NOT RUN {
<!-- %% if(.isMethodsDispatchOn()) { # non-simple use needs 'methods' package -->
# }
# NOT RUN {
f <- function(x, y) {
y <- pmax(y, 0.001)
if (x > 0) x ^ y else stop("x must be positive")
}
## arrange to call the browser on entering and exiting
## function f
trace("f", quote(browser(skipCalls = 4)),
exit = quote(browser(skipCalls = 4)))
## instead, conditionally assign some data, and then browse
## on exit, but only then. Don't bother me otherwise
trace("f", quote(if(any(y < 0)) yOrig <- y),
exit = quote(if(exists("yOrig")) browser(skipCalls = 4)),
print = FALSE)
## Enter the browser just before stop() is called. First, find
## the step numbers
untrace(f) # (as it has changed f's body !)
as.list(body(f))
as.list(body(f)[[3]]) # -> stop(..) is [[4]]
## Now call the browser there
trace("f", quote(browser(skipCalls = 4)), at = list(c(3,4)))
# }
# NOT RUN {
f(-1,2) # --> enters browser just before stop(..)
# }
# NOT RUN {
## trace a utility function, with recover so we
## can browse in the calling functions as well.
trace("as.matrix", recover)
## turn off the tracing (that happened above)
untrace(c("f", "as.matrix"))
# }
# NOT RUN {
## Useful to find how system2() is called in a higher-up function:
trace(base::system2, quote(print(ls.str())))
# }
# NOT RUN {
##-------- Tracing hidden functions : need 'where = *'
##
## 'where' can be a function whose environment is meant:
trace(quote(ar.yw.default), where = ar)
a <- ar(rnorm(100)) # "Tracing ..."
untrace(quote(ar.yw.default), where = ar)
## trace() more than one function simultaneously:
## expression(E1, E2, ...) here is equivalent to
## c(quote(E1), quote(E2), quote(.*), ..)
trace(expression(ar.yw, ar.yw.default), where = ar)
a <- ar(rnorm(100)) # --> 2 x "Tracing ..."
# and turn it off:
untrace(expression(ar.yw, ar.yw.default), where = ar)
# }
# NOT RUN {
## trace calls to the function lm() that come from
## the nlme package.
## (The function nlme is in that package, and the package
## has a namespace, so the where= argument must be used
## to get the right version of lm)
trace(lm, exit = recover, where = asNamespace("nlme"))
# }
# NOT RUN {
<!-- %%}% only if 'methods' -->
# }
Run the code above in your browser using DataLab