addTaskCallback registers an R function that is to be called each time a top-level task is completed. removeTaskCallback un-registers a function that was registered earlier via addTaskCallback. These provide low-level access to the internal/native mechanism for managing task-completion actions. One can use taskCallbackManager at the S-language level to manage S functions that are called at the completion of each task. This is easier and more direct.
addTaskCallback(f, data = NULL, name = character())
removeTaskCallback(id)
| Parameter | Description |
|---|---|
f |
the function that is to be invoked each time a top-level task is successfully completed. This is called with 5 or 4 arguments depending on whether data is specified or not, respectively. The return value should be a logical value indicating whether to keep the callback in the list of active callbacks or discard it. |
data |
if specified, this is the 5-th argument in the call to the callback function f. |
id |
a string or an integer identifying the element in the internal callback list to be removed. Integer indices are 1-based, i.e the first element is 1. The names of currently registered handlers is available using getTaskCallbackNames and is also returned in a call to addTaskCallback. |
name |
character: names to be used. |
# NOT RUN {
times <- function(total = 3, str = "Task a") {
ctr <- 0
function(expr, value, ok, visible) {
ctr <<- ctr + 1
cat(str, ctr, "\n")
keep.me <- (ctr < total)
if (!keep.me)
cat("handler removing itself\n")
# return
keep.me
}
}
# add the callback that will work for
# 4 top-level tasks and then remove itself.
n <- addTaskCallback(times(4))
# now remove it, assuming it is still first in the list.
removeTaskCallback(n)
## See how the handler is called every time till "self destruction":
addTaskCallback(times(4)) # counts as once already
sum(1:10) ; mean(1:3) # two more
sinpi(1) # 4th - and "done"
cospi(1)
tanpi(1)
# }