I would like to design a notification in Shiny to evaluate if the input values is equal to a specific number. The following code example is my attempt. This is a simple app checking if the input value is equal to 1000. If not, a notification will show. I used shinyalert to create the notification and the delay function from the shinyjs package to avoid the situation that execution is too sensitive.
This app works but not perfectly. The problem is when the user types in a number. Sometimes it may execute the notification more than once. I believe it is related to how fast the user type in a number. For example, if I type in 5678 into the numericInput very fast, one notification will show, but if I type in 56 and then follow up with 78 very quickly (within a second). The notification will show twice. The app probably evaluates if 56 equals to 1000, then moves on test if 5678 equals to 1000.
If possible, I hope I can show the notification only once. I thought adding the delay function will help, but it did not, or perhaps I did not use the delay function in the right way. Please let me know if you have any feedback.
library(shiny)
library(shinyjs)
library(shinyalert)
ui <- fluidPage(
useShinyjs(),
useShinyalert(),
titlePanel("Check if the input text is 1000."),
numericInput(inputId = "value_in", value = 0, label = "Input value")
)
server <- function(input, output, session){
observeEvent(input$value_in, {
req(input$value_in)
delay(
ms = 2000,
if (input$value_in != 1000){
shinyalert(text = "The input value is not 1000",
type = "warning")
}
)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
Debounceis the way to go. Below is some more about how it works, and then a minimal working example.