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

Iago Giné Vázquez |@go@g|ne @end|ng |rom @jd@e@
Fri Apr 12 14:53:02 CEST 2024


Thank you Ivan, your example solves my issue this time through

debug(environment(Adder$add)$add)


Just for the future, you say

    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.

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


Best regards,
Iago

________________________________
De: Ivan Krylov <ikrylov using disroot.org>
Enviat el: divendres, 12 d��abril de 2024 14:38
Per a: Iago Gin�� V��zquez <iago.gine using sjd.es>
A/c: r-help using r-project.org <r-help using r-project.org>
Tema: Re: [R] Debugging functions defined (locally) inside another functions

�� 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

	[[alternative HTML version deleted]]



More information about the R-help mailing list