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

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Fri Apr 12 14:38:13 CEST 2024


В Fri, 12 Apr 2024 12:15:07 +0000
Iago Giné Vázquez <iago.gine using 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 function

Can 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



More information about the R-help mailing list