I would like my server to wait until some Javascript returns a result. Consider the following example code
library(shiny)
js <- '
Shiny.addCustomMessageHandler("bar", function(dummy) {
Shiny.setInputValue("uniqueid", "Hello back");
}
'
ui <- shinyUI(fluidPage(tags$script(js)), actionButton("foo", label = "Say hello")))
server <- shinyServer(function(input, output, session) {
observeEvent(input$foo, {
session$sendCustomMessage("bar", "hello")
# it should wait here until uniqueid is no longer NULL
print(input$uniqueid)
})
})
shinyApp(ui, server)
How can this be achieved?
You can add req() to the entry:
library(shiny)
jss <- 'Shiny.addCustomMessageHandler("bar", function(msg) {
Shiny.setInputValue("uniqueid", "Hello back");
})'
ui <- shinyUI(
fluidPage(
tags$script(jss),
actionButton("foo", label = "Say hello")
)
)
server <- shinyServer(function(input, output, session) {
observeEvent(input$foo, {
session$sendCustomMessage("bar", "hello")
req(input$uniqueid)
# it should wait here until uniqueid is no longer NULL
print(input$uniqueid)
})
})
shinyApp(ui, server)