rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.38k stars 1.86k forks source link

Strange behaviour when submitting multiple messages to javascript from Shiny #1181

Closed daattali closed 8 years ago

daattali commented 8 years ago

A few users of shinyjs have reported strange behaviour sometimes when combining an alert message with other javascript functions, and I finally took the time to look into it and it looks like a Shiny issue.

Very minimal example: the following code should have two alert messages one after the other, but the first two times the button is pressed the behaviour is wrong (only one message is shown).

library(shiny)

ui <- fluidPage(
  tags$script(
    "Shiny.addCustomMessageHandler('message', function(params) { alert(params); });"
  ),
  actionButton("btn","Press me")
)
server <- function(input, output, session) {
  observeEvent(input$btn,{
    session$sendCustomMessage('message', 'hello')
    session$sendCustomMessage('message', 'hello again')
  })
}

shinyApp(ui, server)
wch commented 8 years ago

I'm not able to replicate this problem -- whenever I click the button, I get two alerts. Can you include the output of devtools::session_info(), as well as which browser you're using?

Also if you run with options(shiny.trace=TRUE), it will display what's being sent back and forth, and that may help debugging.

bborgesr commented 8 years ago

I get the same result as @wch. It works perfectly fine on the RStudio viewer, Chrome and Safari. For Firefox, I do get the two alerts, but the order is messed up ("hello again" pops up first)... The stack trace shows the correct order though, so that seems to be a Firefox issue...

daattali commented 8 years ago

@wch I didn't know about the trace option, that can be handy in the future, thanks.

I see the problem on my Windows machine, but when I run it on a Linux machine it doesn't happen. The package versions don't exactly match between the two machines, but perhaps it is a windows-only issue.

sessionInfo:

R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252   
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

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

other attached packages:
[1] shinyjs_0.6       shiny_0.13.2.9003

loaded via a namespace (and not attached):
 [1] R6_2.1.2          rsconnect_0.4.2.2 htmltools_0.3.5   tools_3.2.2      
 [5] Rcpp_0.12.4       jsonlite_0.9.19   digest_0.6.9      xtable_1.8-2     
 [9] httpuv_1.3.3      miniUI_0.1.1      mime_0.4   
daattali commented 8 years ago

I saw a related bug in DT, which I now realize is a bug with shiny and not with DT: https://github.com/rstudio/DT/issues/264

daattali commented 8 years ago

@jcheng5 @jjallaire @wch since you're on a roll after today's JS bug fix (https://github.com/rstudio/flexdashboard/issues/29) I just wanted to ping here to see if this could be looked into at some point when you have a bit of time as well

jcheng5 commented 8 years ago

@daattali On my Linux box, I get a repro if I run it within the RStudio captive browser window, but not if I pop it out to Chrome. Are you getting something similar on Windows?

daattali commented 8 years ago

@jcheng5 you're right, I only see the issue in RStudio , but in a real browser it works now. I'm almost certain that when I first reported this bug, I was getting the issue in Chrome as well, but I didn't document that so I can't say that 100%. Anyway, seems ok now, perhaps something got fixed along the way? I'm fine with closing this issue if you don't think it's important enough. Although the fact that it doesn't work inside RStudio probably means that it will still affect shiny gadgets.