addTaskCallback
Manage Top-Level Task Callbacks

Description

These functions manage a list of functions that are called after each top-level expression.

Usage

addTaskCallback(f, data = NULL, name = character())
removeTaskCallback(id)
getTaskCallbackNames()

Arguments

f a logical-valued function with at least four arguments: expr, result, ok, and visible. If the data argument it given to addTaskCallback, then f must have a fifth argument, data.

addTaskCallback adds f to the end of the list of callback functions. After each top-level expression is successfully evaluated, just after possible autoprinting of its result, TERR calls all the callback functions in the list with the top-level expression itself, the result of evaluating it, the constant TRUE (indicating that the evaluation was succcessful), and a logical value indicating whether the result was autoprinted. If the callback was added to the list with along with a data object, then that data object is given to that callback as the fifth argument.

f should return either TRUE or FALSE:

  • FALSE indicates it should be removed from the list of callbacks.
  • TRUE indicates it should continue to be called after the next top-level expression.
If calling f gives an error, then it is removed from the callback list, too.
data an object of any type that is given as the fifth argument to f each time it is called.
name a character string giving the name for the callback. Names do not have to be unique; using the name of an exising callback does not replace the existing one. It just adds another with the same name. If name is not supplied or is NULL, then one is created based on the its position in the list of callbacks.
id an integer or character string specifying the position or name, respectively, of the callback to remove from the list of callbacks.

Details

"Top-level expressions" are those given in response to the TERR prompt. An expression evaluated using the source function are not top-level expressions.
Value
addTaskCallbackreturns the name of the added callback function.
removeTaskCallbackreturns TRUE if a callback was removed and FALSE otherwise (which would happen if id is not a valid index or name in the list of callbacks).
getTaskCallbackNamesreturns a character vector giving the names of the callbacks.
See Also
taskCallbackManager for another interface to the task callback system.
Examples
## Not run: 
addTaskCallback(local({
        N <- 0
        getCpuTime <- function() sum(proc.time()[-3], na.rm=TRUE)
        prevCpuTime <- getCpuTime()
        function(expr, result, ok, visible) {
            N <<- N + 1
            cpuTime <- getCpuTime()
            cat(sep="", "  Task ", N, ": ", round(cpuTime - prevCpuTime,2), " seconds\n")
            prevCpuTime <<- cpuTime
            TRUE
        }
    }),
    name = "showCpuTime")
for(i in 1:10000) for(j in 1:1000) log(i+j)
sum(1:1e4)
removeTaskCallback("showCpuTime")

## End(Not run)
Package base version 6.1.1-7
Package Index