Skip to content
Prev 315385 / 398503 Next

function coverage [solved?]

On Mon, 2013-01-14 at 13:08 -0800, Ross Boylan wrote:
Inspired particularly by Michael Weylandt's suggestion I developed the
following functions, with a typical usage indicated int he comments.
The file is cover.R.  I'm not sure if attachments are allowed, so here
it is inline
-------------------------------------------
# cover.R  determine functions called
# GPL 3.0 license

# Typical use
# mylog <- gzfile("try2.gz", "w")
# monitorNamespace("missreg2", "mylog")
# call one or more functions
# unmonitorNamespace("missreg2")
# close(mylog)

# monitor f (type character, name of function) in environmnet env for calls
# log results to connection con (type character, name of global variable)
monitor <- function(f, con, env=parent.frame()) {
  tin <- parse(text=paste('writeLines("', f, ' in", ', con,")", sep=""))
  tout <- parse(text=paste('writeLines("', f, ' out", ', con,")", sep=""))
  trace(f, tin, tout, print=FALSE, where=env)
  tout
}

unmonitor <- function(f, env=parent.frame()) {
  untrace(f, where=env)
}


# monitor all functions in ns
# con is a global variable that receives the log
monitorNamespace <- function(ns, con){
  e <- getNamespace(ns)
  invisible(sapply(ls(e), function(nm) if (is.function(get(nm, envir=e, inherits=FALSE)))
                   monitor(nm, con, env=e)))
}

unmonitorNamespace <- function(ns){
  e <- getNamespace(ns)
  lapply(ls(e), function(nm) unmonitor(nm, env=e))
}

--------------------------------------------------------------

This mostly seemed to work, but for some reason the outerlevel call I
made is not recorded in the log.  I thought this might be a result of
previous calls to trace or debug, or perhaps my having edited the
function and stuck it in my global namespace.  I tried
detach("missreg2", unload=TRUE") and reloading the library; it didn't
help.  And the function loaation looks like all the others that were
traced:
getAnywhere("rclusbin3")
2 differing objects matching ?rclusbin3? were found
in the following places
  package:missreg2
  namespace:missreg2
which is exactly the same as other functions it did trace.

Thanks also to Duncan and Hadley for their ideas.  I figured with the
profiler I'd always wonder if I'd missed something, and so went this low
tech route.  Because of the missing log entry for the outer level
function, I'm still wondering if I got everything (since the reason the
outer level function wasn't traced might apply to others).