Rev 52927 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/taskCallback.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2011 R Core Development Team% Distributed under GPL 2 or later\name{taskCallback}\alias{addTaskCallback}\alias{removeTaskCallback}\title{Add or Remove a Top-Level Task Callback}\description{\code{addTaskCallback} registers an R functionthat is to be called each time a top-level taskis completed.\code{removeTaskCallback} un-registers a functionthat was registered earlier via \code{addTaskCallback}.These provide low-level access to the internal/nativemechanism for managing task-completion actions.One can use \code{\link{taskCallbackManager}}at the S-language level to manage S functionsthat are called at the completion of each task.This is easier and more direct.}\usage{addTaskCallback(f, data = NULL, name = character(0))removeTaskCallback(id)}\arguments{\item{f}{the function that is to be invoked each time a top-level taskis successfully completed. This is called with 5 or 4 argumentsdepending on whether \code{data} is specified or not, respectively.The return value should be a logical value indicating whether tokeep the callback in the list of active callbacks or discard it.}\item{data}{if specified, this is the 5-th argument in the call to thecallback function \code{f}.}\item{id}{a string or an integer identifying the element in theinternal callback list to be removed.Integer indices are 1-based, i.e the first element is 1.The names of currently registered handlers is availableusing \code{\link{getTaskCallbackNames}}and is also returned in a call to \code{\link{addTaskCallback}}.}\item{name}{character: names to be used.}}\value{\code{addTaskCallback} returnsan integer value giving the position in the listof 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 asno other values are removed from earlier positionsin the list first.\code{removeTaskCallback} returns a logical valueindicating whether the specified element was removed.This can fail (i.e., return \code{FALSE})if an incorrect name or index is given that does notcorrespond to the name or position of an element in the list.}\details{Top-level tasks are individual expressionsrather than entire lines of input. Thus an inputline of the form \code{expression1 ; expression2}will give rise to 2 top-level tasks.A top-level task callback is called with the expression for thetop-level task, the result of the top-level task, a logical valueindicating whether it was successfully completed or not (always TRUEat present), and a logical value indicating whether the result wasprinted or not. If the \code{data} argument was specified in the callto \code{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 tasklist and will not be called again by this mechanism.If the function returns TRUE, it is kept in the list andwill be called on the completion of the next top-level task.}\note{There is also C-level access to top-level task callbacksto allow C routines rather than R functions be used.}\seealso{\code{\link{getTaskCallbackNames}}\code{\link{taskCallbackManager}}\url{http://developer.r-project.org/TaskHandlers.pdf}}\examples{times <- function(total = 3, str="Task a") {ctr <- 0function(expr, value, ok, visible) {ctr <<- ctr + 1cat(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)\dontrun{# There is no point in running this# asaddTaskCallback(times(4))sum(1:10)sum(1:10)sum(1:10)sum(1:10)sum(1:10)}}\keyword{environment}