From murdoch at stats.uwo.ca  Thu Sep 10 13:41:13 2009
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Thu, 10 Sep 2009 07:41:13 -0400
Subject: [Rd] findLineNum and setBreakpoint added
Message-ID: <4AA8E5D9.5060107@stats.uwo.ca>

I've just added a couple of functions to R-devel to help with 
debugging.  findLineNum() finds which line of which function corresponds 
to a particular line of source code; setBreakpoint() takes the output of 
findLineNum, and calls trace() to set a breakpoint there.

These rely on having source reference debug information in the code.  
This is the default for code read by source(), but not for packages.  To 
get the source references in package code, set the environment variable 
R_KEEP_PKG_SOURCE=yes, or within R, set options(keep.source.pkgs=TRUE), 
then install the package from source code.  Read ?findLineNum for 
details on how to
tell it to search within packages, rather than limiting the search to 
the global environment.

For example,

x <- " f <- function(a, b) {
             if (a > b)  {
                 a
             } else {
                 b
             }
         }"

eval(parse(text=x))  # Normally you'd use source() to read a file...

findLineNum("<text>#3")   # <text> is a dummy filename used by parse(text=)

This will print

 f step 2,3,2 in <environment: R_GlobalEnv>

and you can use

setBreakpoint("<text>#3")

to set a breakpoint there.
             
There are still some limitations (and probably bugs) in the code; I'll 
be fixing those over the next while. 

Duncan Murdoch



[Rd] findLineNum and setBreakpoint added
Duncan Murdoch