Andre Zege
2010-Oct-29 21:19 UTC
[R] how to debug (mtrace) a function defined inside a function?
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? -- View this message in context: http://r.789695.n4.nabble.com/how-to-debug-mtrace-a-function-defined-inside-a-function-tp3019781p3019781.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2010-Oct-29 22:01 UTC
[R] how to debug (mtrace) a function defined inside a function?
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
MarkBravington
2010-Dec-05 01:44 UTC
[R] how to debug (mtrace) a function defined inside a function?
Hi Andre Just saw your email; I'm the author of the 'debug' package but I don't subscribe to R-help, so it's best to email me directly with questions about 'debug' or 'mvbutils'. The quick answer is that you can either: - mtrace( mymodel), then after it has executed the 'build <- function()...' line, type mtrace( build) at the prompt - or you can insert the line below into your code of mymodel, so that you won't need to step thru mymodel when it exectues. You could put an 'if' statement based on a global variable if you want to be able to control debugging behaviour "globally". HTH Mark Bravington (mark dot bravington at csiro dot au) mymodel<-function(){ data<-numeric(0) build<-function(){ data<<-1 } ## Add this: mtrace( build) m<-list() m$build<-build m } -- View this message in context: http://r.789695.n4.nabble.com/how-to-debug-mtrace-a-function-defined-inside-a-function-tp3019781p3072957.html Sent from the R help mailing list archive at Nabble.com.