-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
reactiveTimer.Rd
75 lines (66 loc) · 2.28 KB
/
reactiveTimer.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/reactives.R
\name{reactiveTimer}
\alias{reactiveTimer}
\title{Timer}
\usage{
reactiveTimer(intervalMs = 1000, session = getDefaultReactiveDomain())
}
\arguments{
\item{intervalMs}{How often to fire, in milliseconds}
\item{session}{A session object. This is needed to cancel any scheduled
invalidations after a user has ended the session. If \code{NULL}, then
this invalidation will not be tied to any session, and so it will still
occur.}
}
\value{
A no-parameter function that can be called from a reactive context,
in order to cause that context to be invalidated the next time the timer
interval elapses. Calling the returned function also happens to yield the
current time (as in \code{\link[base:Sys.time]{base::Sys.time()}}).
}
\description{
Creates a reactive timer with the given interval. A reactive timer is like a
reactive value, except reactive values are triggered when they are set, while
reactive timers are triggered simply by the passage of time.
}
\details{
\link[=reactive]{Reactive expressions} and observers that want to be
invalidated by the timer need to call the timer function that
\code{reactiveTimer} returns, even if the current time value is not actually
needed.
See \code{\link[=invalidateLater]{invalidateLater()}} as a safer and simpler alternative.
}
\examples{
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
sliderInput("n", "Number of observations", 2, 1000, 500),
plotOutput("plot")
)
server <- function(input, output) {
# Anything that calls autoInvalidate will automatically invalidate
# every 2 seconds.
autoInvalidate <- reactiveTimer(2000)
observe({
# Invalidate and re-execute this reactive expression every time the
# timer fires.
autoInvalidate()
# Do something each time this is invalidated.
# The isolate() makes this observer _not_ get invalidated and re-executed
# when input$n changes.
print(paste("The value of input$n is", isolate(input$n)))
})
# Generate a new histogram each time the timer fires, but not when
# input$n changes.
output$plot <- renderPlot({
autoInvalidate()
hist(rnorm(isolate(input$n)))
})
}
shinyApp(ui, server)
}
}
\seealso{
\code{\link[=invalidateLater]{invalidateLater()}}
}