Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny and JavaScript interaction with session$sendCustomMessage

If I use the session$sendCustomMessage command in my server file and input a list of three things, how do I access the three different things in my message-handler.js file?

Say my call looks like this:

session$sendCustomMessage(type='testmessage', message=list(pid=pid, cid=cid, query=sql))  

In my .js file I want to use pid, cid and query seperately, any ideas on how I do that?

Thanks!!

like image 513
Mandi Avatar asked Sep 23 '14 20:09

Mandi


1 Answers

You would access them as message.pid, message.cid etc. The list is passed as JSON. An example adapted from http://shiny.rstudio.com/gallery/server-to-client-custom-messages.html:

library(shiny)
runApp(
  list(ui = fluidPage(
    titlePanel("sendCustomMessage example"),
    fluidRow(
      column(4, wellPanel(
        sliderInput("controller", "Controller:",
                    min = 1, max = 20, value = 15),
        singleton(
          tags$head(tags$script('Shiny.addCustomMessageHandler("testmessage",
  function(message) {
    alert("The b variable is " + message.b);
  }
);'))
        )
      ))
    )
  )
  , server = function(input, output, session){
    observe({
      session$sendCustomMessage(type = 'testmessage',
                                message = list(a = 1, b = 'text',
                                               controller = input$controller))
    })
  })
)
like image 142
jdharrison Avatar answered Nov 09 '22 06:11

jdharrison