Iago Giné Vázquez
2024-Apr-12 12:15 UTC
[R] Debugging functions defined (locally) inside another functions
Hi all, I am trying to debug an error of a function g defined and used inside another function f of a package. So I have f <- function(whatever){ ... g <- function(whatever2){ ... } ... } If I wanted to debug some thing directly inside f I would do debug(f). But this does not go inside g code. On the other hand, debug(g) does not work as g is not a defined function in the namespace of the package. Is there some way to debug errors inside g? Thank you in advance. All the best, Iago [[alternative HTML version deleted]]
Iago Giné Vázquez
2024-Apr-12 12:22 UTC
[R] Debugging functions defined (locally) inside another functions
To be precise, in the case I am looking this time f is not a function, but f <- ggplot2::ggproto(...) So debug(f) produces Error in debug(f) : argument must be a function Iago ________________________________ De: R-help <r-help-bounces at r-project.org> de part de Iago Gin? V?zquez <iago.gine at sjd.es> Enviat el: divendres, 12 d?abril de 2024 14:15 Per a: r-help at r-project.org <r-help at r-project.org> Tema: [R] Debugging functions defined (locally) inside another functions Hi all, I am trying to debug an error of a function g defined and used inside another function f of a package. So I have f <- function(whatever){ ... g <- function(whatever2){ ... } ... } If I wanted to debug some thing directly inside f I would do debug(f). But this does not go inside g code. On the other hand, debug(g) does not work as g is not a defined function in the namespace of the package. Is there some way to debug errors inside g? Thank you in advance. All the best, Iago [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Ivan Krylov
2024-Apr-12 12:38 UTC
[R] Debugging functions defined (locally) inside another functions
? Fri, 12 Apr 2024 12:15:07 +0000 Iago Gin? V?zquez <iago.gine at sjd.es> ?????:> f <- function(whatever){ > ... > g <- function(whatever2){ > ... > } > ... > } > > If I wanted to debug some thing directly inside f I would do > debug(f). But this does not go inside g code. On the other hand, > debug(g) does not work as g is not a defined function in the > namespace of the package.Moreover, `g` doesn't exist at all until f() is evaluated and reaches this point. If `f` was a function, it would be possible to trace() it, inserting a call to debug(g) after it's created.> f <- ggplot2::ggproto(...) > > So debug(f) produces > Error in debug(f) : argument must be a functionCan you show more information about the call that produces `f`? Where does `g` come into play? Following ?ggplot2::ggproto, I can trigger the browser if I reach into the environment of the publicly available method: Adder <- ggproto(...) # from the example debug(environment(Adder$add)$add) Adder$add(1234) # debugging in: add(..., self = self) # debug ?? #3: { # self$x <- self$x + n # self$x # } -- Best regards, Ivan
Duncan Murdoch
2024-Apr-12 13:36 UTC
[R] Debugging functions defined (locally) inside another functions
On 12/04/2024 8:15 a.m., Iago Gin? V?zquez wrote:> Hi all, I am trying to debug an error of a function g defined and used inside another function f of a package. > So I have > > f <- function(whatever){ > ... > g <- function(whatever2){ > ... > } > ... > } > > If I wanted to debug some thing directly inside f I would do debug(f). But this does not go inside g code. On the other hand, debug(g) does not work as g is not a defined function in the namespace of the package. > > Is there some way to debug errors inside g?The easiest case is if you have access to the source code. Just put a browser() statement at the start of g, i.e. change it to g <- function(whatever2){ browser() ... } and it will break very similarly to what happens if you have set debug(g). Another possibility if you have the source but don't want to edit it is to use trace. Suppose that the definition of g is in source.R at lines 100 to 120. Then you can run setBreakpoint("source.R#101") to set a breakpoint via trace() just before line 101 runs. trace() has lots of options; it can just print things, or call browser(), etc. They are available in setBreakpoint(). If you are executing code from a package and you don't have the source handy it's a bit tedious, but you can still do the search that setBreakpoint() does to find the source. For example, let's set a breakpoint just before the print statement in g in this example: f <- function() { g <- function() { print("this is g") } print("this is f") g() } You need to find the location of that line in f. Look at as.list(body(f)): > as.list(body(f)) [[1]] `{` [[2]] g <- function() { print("this is g") } [[3]] print("this is f") [[4]] g() So we need to look within entry 2: > as.list(body(f)[[2]]) [[1]] `<-` [[2]] g [[3]] function() { print("this is g") } Continue drilling down: > as.list(body(f)[[c(2,3)]]) [[1]] `function` [[2]] NULL [[3]] { print("this is g") } [[4]] function() { print("this is g") } > as.list(body(f)[[c(2,3, 3)]]) [[1]] `{` [[2]] print("this is g") So now we know the print statement is at location c(2,3,3,2). Set a browser call there: > trace(f, at=list(c(2,3,3,2)), tracer = quote(browser())) [1] "f" > body(f) { g <- function() { { .doTrace(browser(), "step 2,3,3,2") print("this is g") } } print("this is f") g() } Note that the "at" argument needs to be a list to drill down; if you just said at=c(2,3,3,2) it would set breakpoints at step 2 and 3. Duncan Murdoch
Seemingly Similar Threads
- Debugging functions defined (locally) inside another functions
- Debugging functions defined (locally) inside another functions
- Debugging functions defined (locally) inside another functions
- Avoiding Delete key function as 'Quit R' in Rterm when there are no characters in cursor line
- Avoiding Delete key function as 'Quit R' in Rterm when there are no characters in cursor line