server.R

The server.R file runs as follows:

function(input, output, session) { 
  output$randomNumber = renderText({ 
    theNumber = sample(1:input$pickNumber, 1) 
    session$sendCustomMessage(type = 'sendMessage',        message = theNumber) 
    return(theNumber) 
  }) 
 
  output$theMessage = renderText({ 
    return(input$JsMessage) 
  }) 
} 

The first thing to note here is the use of function(input, output, session){...} instead of the function(input, output){...} that we are used to seeing. The addition of a session argument adds a considerable amount of functionality to Shiny applications. In this case, it allows us to send messages to JavaScript. There is more on the functionality of the session argument in the next chapter, Chapter 6, Dashboards.

The first function here ...

Get Web Application Development with R Using Shiny - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.