Debian Codegraph @

man/debounce.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/reactives.R
3 \name{debounce}
4 \alias{debounce}
5 \alias{throttle}
6 \title{Slow down a reactive expression with debounce/throttle}
7 \usage{
8 debounce(r, millis, priority = 100, domain = getDefaultReactiveDomain())
9
10 throttle(r, millis, priority = 100, domain = getDefaultReactiveDomain())
11 }
12 \arguments{
13 \item{r}{A reactive expression (that invalidates too often).}
14
15 \item{millis}{The debounce/throttle time window. You may optionally pass a
16 no-arg function or reactive expression instead, e.g. to let the end-user
17 control the time window.}
18
19 \item{priority}{Debounce/throttle is implemented under the hood using
20 \link[=observe]{observers}. Use this parameter to set the priority of
21 these observers. Generally, this should be higher than the priorities of
22 downstream observers and outputs (which default to zero).}
23
24 \item{domain}{See \link{domains}.}
25 }
26 \description{
27 Transforms a reactive expression by preventing its invalidation signals from
28 being sent unnecessarily often. This lets you ignore a very "chatty" reactive
29 expression until it becomes idle, which is useful when the intermediate
30 values don't matter as much as the final value, and the downstream
31 calculations that depend on the reactive expression take a long time.
32 \code{debounce} and \code{throttle} use different algorithms for slowing down
33 invalidation signals; see Details.
34 }
35 \details{
36 This is not a true debounce/throttle in that it will not prevent \code{r}
37 from being called many times (in fact it may be called more times than
38 usual), but rather, the reactive invalidation signal that is produced by
39 \code{r} is debounced/throttled instead. Therefore, these functions should be
40 used when \code{r} is cheap but the things it will trigger (downstream
41 outputs and reactives) are expensive.
42
43 Debouncing means that every invalidation from \code{r} will be held for the
44 specified time window. If \code{r} invalidates again within that time window,
45 then the timer starts over again. This means that as long as invalidations
46 continually arrive from \code{r} within the time window, the debounced
47 reactive will not invalidate at all. Only after the invalidations stop (or
48 slow down sufficiently) will the downstream invalidation be sent.
49
50 \verb{ooo-oo-oo---- => -----------o-}
51
52 (In this graphical depiction, each character represents a unit of time, and
53 the time window is 3 characters.)
54
55 Throttling, on the other hand, delays invalidation if the \emph{throttled}
56 reactive recently (within the time window) invalidated. New \code{r}
57 invalidations do not reset the time window. This means that if invalidations
58 continually come from \code{r} within the time window, the throttled reactive
59 will invalidate regularly, at a rate equal to or slower than the time
60 window.
61
62 \verb{ooo-oo-oo---- => o--o--o--o---}
63 }
64 \section{Limitations}{
65
66
67 Because R is single threaded, we can't come close to guaranteeing that the
68 timing of debounce/throttle (or any other timing-related functions in
69 Shiny) will be consistent or accurate; at the time we want to emit an
70 invalidation signal, R may be performing a different task and we have no
71 way to interrupt it (nor would we necessarily want to if we could).
72 Therefore, it's best to think of the time windows you pass to these
73 functions as minimums.
74
75 You may also see undesirable behavior if the amount of time spent doing
76 downstream processing for each change approaches or exceeds the time
77 window: in this case, debounce/throttle may not have any effect, as the
78 time each subsequent event is considered is already after the time window
79 has expired.
80 }
81
82 \examples{
83 ## Only run examples in interactive R sessions
84 if (interactive()) {
85 options(device.ask.default = FALSE)
86
87 library(shiny)
88 library(magrittr)
89
90 ui <- fluidPage(
91 plotOutput("plot", click = clickOpts("hover")),
92 helpText("Quickly click on the plot above, while watching the result table below:"),
93 tableOutput("result")
94 )
95
96 server <- function(input, output, session) {
97 hover <- reactive({
98 if (is.null(input$hover))
99 list(x = NA, y = NA)
100 else
101 input$hover
102 })
103 hover_d <- hover \%>\% debounce(1000)
104 hover_t <- hover \%>\% throttle(1000)
105
106 output$plot <- renderPlot({
107 plot(cars)
108 })
109
110 output$result <- renderTable({
111 data.frame(
112 mode = c("raw", "throttle", "debounce"),
113 x = c(hover()$x, hover_t()$x, hover_d()$x),
114 y = c(hover()$y, hover_t()$y, hover_d()$y)
115 )
116 })
117 }
118
119 shinyApp(ui, server)
120 }
121
122 }