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