Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Application Development with R Using Shiny

You're reading from   Web Application Development with R Using Shiny Build stunning graphics and interactive data visualizations to deliver cutting-edge analytics

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher
ISBN-13 9781788993128
Length 238 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Chris Beeley Chris Beeley
Author Profile Icon Chris Beeley
Chris Beeley
Shitalkumar R. Sukhdeve Shitalkumar R. Sukhdeve
Author Profile Icon Shitalkumar R. Sukhdeve
Shitalkumar R. Sukhdeve
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Beginning R and Shiny FREE CHAPTER 2. Shiny First Steps 3. Integrating Shiny with HTML 4. Mastering Shiny's UI Functions 5. Easy JavaScript and Custom JavaScript Functions 6. Dashboards 7. Power Shiny 8. Code Patterns in Shiny Applications 9. Persistent Storage and Sharing Shiny Applications 10. Other Books You May Enjoy

Debounce and throttle

Debounce and throttle are used to slow down a reactive expression. For example, suppose we are using the invalidation check for a reactive expression and error indications are prompted unnecessarily. We can use debounce and throttle to make expressions such as these slow down and wait for intermediate expressions to complete their calculations. The syntaxes of both of these are as follows:

debounce(r, millis, priority = 100, domain = getDefaultReactiveDomain()) 
 
throttle(r, millis, priority = 100, domain = getDefaultReactiveDomain()) 

Here, r is the reactive expression that invalidates too often. millis is the time window used by debounce/throttle, and priority sets the observer's priority. For example, if we want to add debounce to an expression, we can do it as follows:

plot_iris<-  plot(iris$Sepal.Length,iris$Sepal.Width) ) %>% debounce(1000) 
For more detail visite https://shiny.rstudio.com/reference/shiny/1.0.0/debounce.html. Lets have an example 
 
## Only run examples in interactive R sessions 
if (interactive()) { 
  options(device.ask.default = FALSE) 
   
  library(shiny) 
  library(magrittr) 
   
  ui <- fluidPage( 
    plotOutput("plot", click = clickOpts("hover")), 
    helpText("Quickly click on the plot above, while watching the result table below:"), 
    tableOutput("result") 
  ) 
   
  server <- function(input, output, session) { 
    hover <- reactive({ 
      if (is.null(input$hover)) 
        list(x = NA, y = NA) 
      else 
        input$hover 
    }) 
    hover_d <- hover %>% debounce(1000) 
    hover_t <- hover %>% throttle(1000) 
     
    output$plot <- renderPlot({ 
      plot(iris) 
    }) 
     
    output$result <- renderTable({ 
      data.frame( 
        mode = c("raw", "throttle", "debounce"), 
        x = c(hover()$x, hover_t()$x, hover_d()$x), 
        y = c(hover()$y, hover_t()$y, hover_d()$y) 
      ) 
    }) 
  } 
   
  shinyApp(ui, server) 
} 

You will get the following output:

Result table
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image