[R] Debugging functions defined (locally) inside another functions

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Fri Apr 12 15:32:17 CEST 2024


В Fri, 12 Apr 2024 12:53:02 +0000
Iago Giné Vázquez <iago.gine using sjd.es> пишет:

> How should I call trace() if f was a function?

Let the tracer be quote(debug(g)) and use as.list(body(f)) to determine
where it should be injected:

f <- function() {
 message('exists("g") so far is ', exists('g'))
 g <- function() {
  flag <- TRUE
  if (flag) stop('an error')
 }
 message('about to run g()')
 g()
}

In this example, step number 4 is message("about to run g()"), so
injecting a call to debug() before it should work:

trace(f, quote(debug(g)), at = 4)
f()
# exists("g") so far is FALSE
# Tracing f() step 4 # <-- at this point debug(g) is run
# about to run g()
# debugging in: g()
# debug at #3: {
#    flag <- TRUE
#     if (flag)
#         stop("an error")
# }

help(trace) has an extensive example showing how to use it for many
similar purposes.

-- 
Best regards,
Ivan



More information about the R-help mailing list