| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/reactives.R |
| 3 |
\name{invalidateLater} |
| 4 |
\alias{invalidateLater} |
| 5 |
\title{Scheduled Invalidation} |
| 6 |
\usage{ |
| 7 |
invalidateLater(millis, session = getDefaultReactiveDomain()) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{millis}{Approximate milliseconds to wait before invalidating the |
| 11 |
current reactive context.} |
| 12 |
|
| 13 |
\item{session}{A session object. This is needed to cancel any scheduled |
| 14 |
invalidations after a user has ended the session. If \code{NULL}, then |
| 15 |
this invalidation will not be tied to any session, and so it will still |
| 16 |
occur.} |
| 17 |
} |
| 18 |
\description{ |
| 19 |
Schedules the current reactive context to be invalidated in the given number |
| 20 |
of milliseconds. |
| 21 |
} |
| 22 |
\details{ |
| 23 |
If this is placed within an observer or reactive expression, that object will |
| 24 |
be invalidated (and re-execute) after the interval has passed. The |
| 25 |
re-execution will reset the invalidation flag, so in a typical use case, the |
| 26 |
object will keep re-executing and waiting for the specified interval. It's |
| 27 |
possible to stop this cycle by adding conditional logic that prevents the |
| 28 |
\code{invalidateLater} from being run. |
| 29 |
} |
| 30 |
\examples{ |
| 31 |
## Only run examples in interactive R sessions |
| 32 |
if (interactive()) { |
| 33 |
|
| 34 |
ui <- fluidPage( |
| 35 |
sliderInput("n", "Number of observations", 2, 1000, 500), |
| 36 |
plotOutput("plot") |
| 37 |
) |
| 38 |
|
| 39 |
server <- function(input, output, session) { |
| 40 |
|
| 41 |
observe({ |
| 42 |
# Re-execute this reactive expression after 1000 milliseconds |
| 43 |
invalidateLater(1000, session) |
| 44 |
|
| 45 |
# Do something each time this is invalidated. |
| 46 |
# The isolate() makes this observer _not_ get invalidated and re-executed |
| 47 |
# when input$n changes. |
| 48 |
print(paste("The value of input$n is", isolate(input$n))) |
| 49 |
}) |
| 50 |
|
| 51 |
# Generate a new histogram at timed intervals, but not when |
| 52 |
# input$n changes. |
| 53 |
output$plot <- renderPlot({ |
| 54 |
# Re-execute this reactive expression after 2000 milliseconds |
| 55 |
invalidateLater(2000) |
| 56 |
hist(rnorm(isolate(input$n))) |
| 57 |
}) |
| 58 |
} |
| 59 |
|
| 60 |
shinyApp(ui, server) |
| 61 |
} |
| 62 |
} |
| 63 |
\seealso{ |
| 64 |
\code{\link[=reactiveTimer]{reactiveTimer()}} is a slightly less safe alternative. |
| 65 |
} |