shiny icon indicating copy to clipboard operation
shiny copied to clipboard

`shiny:conditional` is running multiple times during the flush cycle

Open krystian8207 opened this issue 2 years ago • 0 comments

System details

Browser Version: Chrome, Version 103.0.5060.114 (Official Build) (64-bit)

Output of sessionInfo():

R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shinyGizmo_0.2.2 shiny_1.7.1     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7         knitr_1.36         magrittr_2.0.1     xtable_1.8-4       R6_2.5.1           rlang_1.0.2        fastmap_1.1.0      shinyWidgets_0.7.0
 [9] tools_4.1.0        xfun_0.31          cli_3.3.0          jquerylib_0.1.4    htmltools_0.5.2    ellipsis_0.3.2     yaml_2.2.1         digest_0.6.28     
[17] lifecycle_1.0.1    purrr_0.3.4        later_1.3.0        sass_0.4.0         promises_1.2.0.1   cachem_1.0.6       glue_1.6.2         evaluate_0.14     
[25] mime_0.12          rmarkdown_2.14     compiler_4.1.0     bslib_0.3.1        jsonlite_1.7.2     httpuv_1.6.3

Example application or steps to reproduce the problem

library(shiny)
ui <- fluidPage(
  tags$head(
    tags$script("$(document).on('shiny:conditional', function(event) {console.log(event);})")
  ),
  actionButton("val", "Value"),
  textOutput("out")
)
server <- function(input, output, session) {
  output$out <- renderText({
    input$val
  })
}
shinyApp(ui, server)

Describe the problem in detail

Looking at the browser console we may realize the event is triggered multiple times. This happens due to the usage of inputConditionals in multiple places (e.g. while running sendInput or dispatchMessage).

A possible solution could be to trigger the function only when the application flush cycle is finished.

krystian8207 avatar Jul 12 '22 12:07 krystian8207