The message is not printed after the button is clicked

Viewed 29

I can't generate a popup message with my code. Here is a snippet of the code:

# server.R

library(shiny)


shinyServer(function(input, output, session) {
    observeEvent(input$btn_enter, {
        session$sendCustomMessage(type = 'testmessage', message = 'You clicked the button!')
    })
})

# ui.R

library(shiny)

shinyUI(fluidPage(
    actionButton(
        "btn_enter",
        "Enter"
    )
))

It is probably a trivial thing, but what am I missing?

1 Answers

I tryed your code and it was not working for me. But I found a workaround with javascript:

# ui.R

library(shiny)

ui <- shinyUI(fluidPage(
          tags$head(tags$script(HTML('Shiny.addCustomMessageHandler("jsCode",function(message) {eval(message.value);});'))),
          actionButton(
                    "btn_enter",
                    "Enter"
          )))

# server.R

server <- shinyServer(function(input, output, session) {
          observeEvent(input$btn_enter, {
                    js_string <- 'alert("You clicked the button!");'
                    session$sendCustomMessage(type='jsCode', list(value = js_string))          })
})

shinyApp(ui, server)
Related