How to untrace multiple functions

debugging, r, trace

Solution

Well, change to:

untrace_my_fns <- function()
{
  fn_names <- c("f", "g", "h")
  invisible(untrace(
    fn_names,
    where = globalenv()
  ))
}

untrace_my_fns()
set.seed(4)
f() # no more tracing...

...because you called `trace` instead of `untrace`!

Problem

If I have a few interconnected functions, then it can be useful to trace the path through them. That is using the `trace` function to give a message each time R enters or leaves one of the functions. For example, ``` f <- function() g() g <- function() h() h <- function() { if(runif(1) > 0.3) g() else 99 } trace_my_fns <- function() { fn_names <- c("f", "g", "h") invisible(trace( fn_names, tracer = quote(0), exit = quote(0), where = globalenv() )) } trace_my_fns() set.seed(4) f() ``` When I'm finished with this tracing, I need to untrace them. ``` untrace_my_fns <- function() { fn_names <- c("f", "g", "h") invisible(trace( fn_names, where = globalenv() )) } untrace_my_fns() ``` For some reason, this isn't untracing the functions properly. To see this, take a look at ``` f body(f) ``` If I directly call untrace on each function, for example `untrace(f)` at the commmand line, it works. How should I create a function to untrace all my functions at once?

Original source