简体   繁体   中英

sendCustomMessage does not work properly in actionButton (Shiny)

I am testing the script from here http://shiny.rstudio.com/articles/action-buttons.html (see the section "Pattern 1 - Command").

If to press the button "Click me" on the site so everything is Ok - we can see the popup menu.

But if to copy the example script into new .R file and run it - no popup message appeared, no warning or errror message is generated (my brouser is Google Chrome). So I am stalled with it.

The example script:

library(shiny)

ui <- fluidPage(
  tags$head(tags$script(src = "message-handler.js")),
  actionButton("do", "Click Me")
)

server <- function(input, output, session) {
  observeEvent(input$do, {
    session$sendCustomMessage(type = 'testmessage',
      message = 'Thank you for clicking')
  })
}

shinyApp(ui, server) 

This should work, I gave two examples of pop-up alerts

1) With standard js alert

rm(list = ls())
library(shiny)
ui <- fluidPage(
  tags$head(tags$script(HTML('Shiny.addCustomMessageHandler("jsCode",function(message) {eval(message.value);});'))),
  actionButton("do", "Click Me")
)
server <- function(input, output, session) {
  observeEvent(input$do, {
    js_string <- 'alert("Thank you for clicking");'
    session$sendCustomMessage(type='jsCode', list(value = js_string))
  })
}

shinyApp(ui, server) 

在此处输入图片说明

2) Using shinyBS package and modal pop-up

rm(list = ls())
library(shiny)
library(shinyBS)

ui <- fluidPage(  
  tags$script(HTML('Shiny.addCustomMessageHandler("jsCode",function(message) {eval(message.value);});')),
  bsModal("ThankYou", "Message", "",tags$p(tags$h1("Thank you for clicking", style = "color:red", align = "center")), size = "small"),
  actionButton("do", "Click Me")
)
server <- function(input, output, session) {
  observeEvent(input$do, {
    activate_modal <- "$('#ThankYou').modal('show')"
    session$sendCustomMessage(type='jsCode', list(value = activate_modal))
  })
}
shinyApp(ui, server) 

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM