The R Project SVN R

Rev

Rev 61150 | Rev 68480 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/taskCallback.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
17067 duncan 6
\name{taskCallback}
7
\alias{addTaskCallback}
8
\alias{removeTaskCallback}
50821 ripley 9
\title{Add or Remove a Top-Level Task Callback}
20029 duncan 10
\description{
11
  \code{addTaskCallback} registers an R function
12
  that is to be called each time a top-level task
13
  is completed.
14
 
15
  \code{removeTaskCallback} un-registers a function
16
  that was registered earlier via \code{addTaskCallback}.
17
 
18
  These provide low-level access to the internal/native
19
  mechanism for managing task-completion actions.
20
  One can use \code{\link{taskCallbackManager}}
21
  at the S-language level to manage S functions
22
  that are called at the completion of each task.
23
  This is easier and more direct.
24
}
17067 duncan 25
\usage{
53820 ripley 26
addTaskCallback(f, data = NULL, name = character())
17067 duncan 27
removeTaskCallback(id)
28
}
29
\arguments{
30
  \item{f}{the function that is to be invoked each time a top-level task
55555 ripley 31
    is successfully completed.  This is called with 5 or 4 arguments
17067 duncan 32
    depending on whether \code{data} is specified or not, respectively.
33
    The return value should be a logical value indicating whether to
34
    keep the callback in the list of active callbacks or discard it.}
35
  \item{data}{if specified, this is the 5-th argument in the call to the
36
    callback function \code{f}.}
37
  \item{id}{a string or an integer identifying the element in the
38
    internal callback list to be removed.
39
    Integer indices are 1-based, i.e the first element is 1.
40
    The names of currently registered handlers is available
41
    using \code{\link{getTaskCallbackNames}}
42
    and is also returned in a call to \code{\link{addTaskCallback}}.
43
  }
17200 ripley 44
  \item{name}{character: names to be used.}
17067 duncan 45
}
46
\value{
47
  \code{addTaskCallback} returns
48
  an integer value giving  the position in the list
49
  of task callbacks that this new callback occupies.
50
  This is only the current position of the callback.
51
  It can be used to remove the entry as long as
52
  no other values are removed from earlier positions
53
  in the list first.
54
 
55
  \code{removeTaskCallback} returns a logical value
56
  indicating whether the specified element was removed.
25099 hornik 57
  This can fail (i.e., return \code{FALSE})
17067 duncan 58
  if an incorrect name or index is given that does not
59
  correspond to the name or position of an element in the list.
60
}
61
\details{
62
Top-level tasks are individual expressions
55555 ripley 63
rather than entire lines of input.  Thus an input
17067 duncan 64
line of the form \code{expression1 ; expression2}
65
will give rise to 2 top-level tasks.
61433 ripley 66
 
17067 duncan 67
A top-level task callback is called with the expression for the
68
top-level task, the result of the top-level task, a logical value
69
indicating whether it was successfully completed or not (always TRUE
70
at present), and a logical value indicating whether the result was
55555 ripley 71
printed or not.  If the \code{data} argument was specified in the call
17067 duncan 72
to \code{addTaskCallback}, that value is given as the fifth argument.
73
 
74
The callback function should return a logical value.
75
If the value is FALSE, the callback is removed from the task
76
list and will not be called again by this mechanism.
77
If the function returns TRUE, it is kept in the list and
78
will be called on the completion of the next top-level task.
79
}
80
\note{
81
  There is also C-level access to top-level task callbacks
82
  to allow C routines rather than R functions be used.
83
}
84
\seealso{
85
  \code{\link{getTaskCallbackNames}}
86
  \code{\link{taskCallbackManager}}
87
  \url{http://developer.r-project.org/TaskHandlers.pdf}
88
}
89
\examples{
61150 ripley 90
times <- function(total = 3, str = "Task a") {
30915 ripley 91
   ctr <- 0
17067 duncan 92
 
30915 ripley 93
   function(expr, value, ok, visible) {
94
    ctr <<- ctr + 1
95
    cat(str, ctr, "\n")
96
    if(ctr == total) {
97
      cat("handler removing itself\n")
17067 duncan 98
    }
30915 ripley 99
    return(ctr < total)
100
   }
101
 }
17067 duncan 102
 
30915 ripley 103
 # add the callback that will work for
104
 # 4 top-level tasks and then remove itself.
105
 n <- addTaskCallback(times(4))
17067 duncan 106
 
30915 ripley 107
 # now remove it, assuming it is still first in the list.
108
 removeTaskCallback(n)
17067 duncan 109
 
110
\dontrun{
30915 ripley 111
# There is no point in running this
61433 ripley 112
# as
30915 ripley 113
 addTaskCallback(times(4))
17067 duncan 114
 
30915 ripley 115
 sum(1:10)
116
 sum(1:10)
117
 sum(1:10)
118
 sum(1:10)
119
 sum(1:10)
17067 duncan 120
}
121
}
122
\keyword{environment}