Is there any syntactic sugar for using debounce/throttle on an input value?

285 views
Skip to first unread message

Dean Attali

unread,
Aug 2, 2017, 4:28:17 PM8/2/17
to Shiny - Web Framework for R
Suppose I have a numeric input with ID "num" and in the server I want to refer to `input$num`. I want it to fire no more than once every second, so I want to use the `throttle(r, 1000)` function.

This is the code I would:


num <- reactive(input$num)
num_t
<- throttle(num, 1000)
observe
({
  cat
(num_t())
})


I was wondering if there's a more compact way to do this, without having to define a variable that only gets used in one place?

Joe Cheng

unread,
Aug 2, 2017, 5:07:20 PM8/2/17
to Dean Attali, Shiny - Web Framework for R
Is this what you mean?

num <- reactive(input$num) %>% throttle(1000)

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/b7ea3a90-6214-4eb4-be25-770a90e0a115%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dean Attali

unread,
Aug 2, 2017, 5:13:06 PM8/2/17
to Joe Cheng, Shiny - Web Framework for R
This is indeed what the documentation has, but it still requires me to define a separate variable.

There were two things I ideally wanted:
- be able to pass `input$num` into the throttle() function instead of having to decorate it with a reactive(), but I understand if that's not possible. 
- define the entire expression inline without declaring a variable, for example cat(throttle(reactive(input$num), 1000)())

The fact that we even have debounce/throttle is already huge, I've been spoiled already so I understand if these are not possible

---
Dean Attali
President & CEO
AttaliTech Ltd

On 2 August 2017 at 17:07, Joe Cheng <j...@rstudio.com> wrote:
Is this what you mean?

num <- reactive(input$num) %>% throttle(1000)

On Wed, Aug 2, 2017 at 1:28 PM Dean Attali <daat...@gmail.com> wrote:
Suppose I have a numeric input with ID "num" and in the server I want to refer to `input$num`. I want it to fire no more than once every second, so I want to use the `throttle(r, 1000)` function.

This is the code I would:


num <- reactive(input$num)
num_t
<- throttle(num, 1000)
observe
({
  cat
(num_t())
})


I was wondering if there's a more compact way to do this, without having to define a variable that only gets used in one place?

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.

Joe Cheng

unread,
Aug 2, 2017, 6:07:52 PM8/2/17
to Dean Attali, Shiny - Web Framework for R
Oh, I see what you mean. There does need to be a separate variable outside of the observer, otherwise you'd be creating a new throttling "object" with every run of the observer, with no memory of the past. (Hmmm, that might actually work for debounce, but definitely not for throttle.)

To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages