Skip to content

How do I step thru all lines (including step into sub-routines) in a R script?

11 messages · Duncan Murdoch, William Dunlap, Michael

#
On 12-11-13 4:05 PM, Michael wrote:
Mark all your functions for debugging using debug().  It's not like a 
debugger that will single step until you tell it to just continue on, 
but it gives you the individual steps.

When you've had enough of that, use undebug() to mark the functions for 
stepping over.

Duncan Murdoch
#
On 12-11-13 4:50 PM, Michael wrote:
When you see you are about to enter one that you haven't marked, you can 
mark it from within the debugger.  (So in some other debuggers you'd 
type "s" to step in; in R you need to type "debug(foo)" then "n".

Duncan Murdoch
#
On 12-11-13 5:07 PM, Michael wrote:
Not a simple one.  You could try parsing all the scripts, and look for 
function definitions, and then see where those were assigned.

It would be easier to organize your scripts so that some of them define 
functions, and others call them.  Then just define all the functions, 
and use ls() to find their names.

Duncan Murdoch
#
You can make a list of the names of the functions defined at the top
level in a script using the following.

namesOfFunctionsDefined <- function (expr) { # expr is typically output of parse(file)
    expr <- as.list(expr)
    isFunctionAssignment <- function(expr) is.call(expr) && identical(expr[[1]],
        as.name("<-")) && is.call(expr[[3]]) && identical(expr[[3]][[1]],
        as.name("function"))
    asgns <- vapply(expr, isFunctionAssignment, FALSE)
    expr <- expr[asgns]
    vapply(expr, function(e) deparse(e[[2]])[1], "")
}

E.g., for the script /tmp/r.R containing
  f1 <- function(x)x+1
  x <- 10
  x1 <- f1(x)
  f2 <- function(x)x+1
  x2 <- f2(x)
I get:
  > namesOfFunctionsDefined(parse("/tmp/r.R"))
  [1] "f1" "f2"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com