[R] how to debug (mtrace) a function defined inside a function?

Duncan Murdoch murdoch.duncan at gmail.com
Sat Oct 30 00:01:34 CEST 2010


On 29/10/2010 5:19 PM, Andre Zege wrote:
>
> Hi, everyone. I am using a fair amount of closures in my code. Problem i am
> experiencing is i cannot figure out how to mtrace functions defined within a
> function. There must be some way to name such function for mtrace to see it
> and let me step into it. For example, say i have code
>
>
> mymodel<-function(){
>   data<-numeric(0)
>   build<-function(){
>     data<<-1
>   }
>
>   m<-list()
>   m$build<-build
>   m
> }
>
>
> How do I mtrace build function defined inside mymodel function so that i can
> step into build?

I don't use mtrace, but you can use setBreakpoint() to set a breakpoint 
by line number, and presumably you could set the action to whatever will 
trigger mtrace.  It will modify the source of mymodel so that every time 
it creates a new build(), it creates it with a breakpoint within.

See ?setBreakpoint and ?trace.

Duncan Murdoch



More information about the R-help mailing list