Chapter 3 JS in Shiny

3.1 Add JS to Shiny

How to add JavaScript to {shiny}?

3.1.1 With {golem}

golem::add_js_file()

Simple JS file

golem::add_js_handler()

Shiny Handlers

3.2 The JavaScript Shiny Object

In the browser, there is a Shiny JavaScript object.

3.2.1 Set the value of an input from JavaScript

  • From your browser, you can set the value of a Shiny input:
Shiny.setInputValue(id, value, options)

With {priority : 'event'}, the value is sent even if the value hasn’t changed (not the default behavior)

Shiny.setInputValue("pouet", 12, {priority : 'event'})

pouet will appear as input$pouet

observeEvent( input$pouet , {

})

3.3 Shiny JS Handlers

  • Pass data from R to JS (from server to browser)
Shiny.addCustomMessageHandler('alert', function(arg) {
  alert(arg.val);
  alert(arg.size);
});
  • Send with session$sendCustomMessage()

  • To pass multiple value, pass a list, then access with .name

session$sendCustomMessage("alert", list(
  val = 2, 
  size = 11
))

ThinkR Website