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)
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.f
.getTaskCallbackNames
and is also returned in a call to addTaskCallback
.
addTaskCallback
returns
an integer value giving the position in the list
of task callbacks that this new callback occupies.
This is only the current position of the callback.
It can be used to remove the entry as long as
no other values are removed from earlier positions
in the list first.removeTaskCallback
returns a logical value
indicating whether the specified element was removed.
This can fail (i.e., return FALSE
)
if an incorrect name or index is given that does not
correspond to the name or position of an element in the list.
expression1 ; expression2
will give rise to 2 top-level tasks.A top-level task callback is called with the expression for the
top-level task, the result of the top-level task, a logical value
indicating whether it was successfully completed or not (always TRUE
at present), and a logical value indicating whether the result was
printed or not. If the data
argument was specified in the call
to addTaskCallback
, that value is given as the fifth argument.
The callback function should return a logical value. If the value is FALSE, the callback is removed from the task list and will not be called again by this mechanism. If the function returns TRUE, it is kept in the list and will be called on the completion of the next top-level task.
getTaskCallbackNames
taskCallbackManager
http://developer.r-project.org/TaskHandlers.pdf
times <- function(total = 3, str = "Task a") {
ctr <- 0
function(expr, value, ok, visible) {
ctr <<- ctr + 1
cat(str, ctr, "\n")
if(ctr == total) {
cat("handler removing itself\n")
}
return(ctr < total)
}
}
# 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)
## Not run:
# # There is no point in running this
# # as
# addTaskCallback(times(4))
#
# sum(1:10)
# sum(1:10)
# sum(1:10)
# sum(1:10)
# sum(1:10)
# ## End(Not run)
Run the code above in your browser using DataLab