The R Project SVN R

Rev

Rev 87286 | 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
68948 ripley 2
% Part of the R package, https://www.R-project.org
78539 ripley 3
% Copyright 1995-2020 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}}
78539 ripley 21
  at the \R-language level to manage \R functions
20029 duncan 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
87653 smeyer 41
    using \code{\link{getTaskCallbackNames}()}
42
    and is also returned in a call to \code{addTaskCallback}.
17067 duncan 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{
87286 smeyer 85
  \code{\link{getTaskCallbackNames}},
17067 duncan 86
  \code{\link{taskCallbackManager}}
87286 smeyer 87
 
70260 ripley 88
  \url{https://developer.r-project.org/TaskHandlers.pdf}
17067 duncan 89
}
90
\examples{
61150 ripley 91
times <- function(total = 3, str = "Task a") {
68199 maechler 92
  ctr <- 0
93
  function(expr, value, ok, visible) {
30915 ripley 94
    ctr <<- ctr + 1
95
    cat(str, ctr, "\n")
68199 maechler 96
    keep.me <- (ctr < total)
97
    if (!keep.me)
30915 ripley 98
      cat("handler removing itself\n")
17067 duncan 99
 
68199 maechler 100
    # return
101
    keep.me
102
  }
103
}
17067 duncan 104
 
68199 maechler 105
# add the callback that will work for
106
# 4 top-level tasks and then remove itself.
107
n <- addTaskCallback(times(4))
17067 duncan 108
 
68199 maechler 109
# now remove it, assuming it is still first in the list.
110
removeTaskCallback(n)
17067 duncan 111
 
68480 ripley 112
## See how the handler is called every time till "self destruction":
68199 maechler 113
 
114
addTaskCallback(times(4)) # counts as once already
115
 
116
sum(1:10) ; mean(1:3) # two more
117
sinpi(1)              # 4th - and "done"
118
cospi(1)
119
tanpi(1)
17067 duncan 120
}
121
\keyword{environment}