shiny Javascript API Sending data from server to client

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


In many instances, you will want to send data from the R server to the JS client. Here is a very simple example:

    ui = fluidPage(
        "Shiny.addCustomMessageHandler('message', function(params) { alert(params); });"  
      actionButton("btn","Press Me")
    server = function(input, output, session) {
        randomNumber <- runif(1,0,100)
        session$sendCustomMessage("message",list(paste0(randomNumber," is a random number!")))

The workhorses here are the session$sendCustomMessage function in R and the Shiny.addCustomMessageHandler function in javascript.

The session$sendCustomMessage function lets you send parameters from R to a javascript function, and Shiny.addCustomMessageHandler let's you define the javascript function that accepts the parameters from R.

Note: Lists are converted to JSON when they are passed from R to javascript

Got any shiny Question?