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

2024-04-12 Thread Iago Giné Vázquez
Thanks a lot both Duncan and Ivan,

I will keep that example in mind, Duncan, great!

Best  regards,
Iago


De: Duncan Murdoch 
Enviat el: divendres, 12 d�abril de 2024 15:36
Per a: Iago Gin� V�zquez ; r-help@r-project.org 

Tema: Re: [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

[[alternative HTML version deleted]]

__
R-help@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.


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

2024-04-12 Thread Duncan Murdoch

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

__
R-help@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.


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

2024-04-12 Thread Ivan Krylov via R-help
В Fri, 12 Apr 2024 12:53:02 +
Iago Giné Vázquez  пишет:

> 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

__
R-help@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.


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

2024-04-12 Thread Iago Giné Vázquez
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 
Enviat el: divendres, 12 d��abril de 2024 14:38
Per a: Iago Gin�� V��zquez 
A/c: r-help@r-project.org 
Tema: Re: [R] Debugging functions defined (locally) inside another functions

�� Fri, 12 Apr 2024 12:15:07 +
Iago Gin�� V��zquez  ��ڧ�֧�:

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

__
R-help@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.


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

2024-04-12 Thread Ivan Krylov via R-help
В Fri, 12 Apr 2024 12:15:07 +
Iago Giné Vázquez  пишет:

> 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

__
R-help@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.


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

2024-04-12 Thread Iago Giné Vázquez
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  de part de Iago Gin� V�zquez 

Enviat el: divendres, 12 d�abril de 2024 14:15
Per a: r-help@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@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]]

__
R-help@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.


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

2024-04-12 Thread Iago Giné Vázquez
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@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.