clientsideFunction: Define a clientside callback

View source: R/utils.R

clientsideFunctionR Documentation

Define a clientside callback

Description

Create a callback that updates the output by calling a clientside (JavaScript) function instead of an R function. Note that it is also possible to specify JavaScript as a character string instead of passing clientsideFunction. In this case Dash will inline your JavaScript automatically, without needing to save a script inside assets.

Usage

clientsideFunction(namespace, function_name)

Arguments

namespace

Character. Describes where the JavaScript function resides (Dash will look for the function at window[namespace][function_name].)

function_name

Character. Provides the name of the JavaScript function to call.

Details

With this signature, Dash's front-end will call window.my_clientside_library.my_function with the current values of the value properties of the components my-input and another-input whenever those values change. Include a JavaScript file by including it your assets/ folder. The file can be named anything but you'll need to assign the function's namespace to the window. For example, this file might look like:

window.my_clientside_library = {
my_function: function(input_value_1, input_value_2) {
   return (
     parseFloat(input_value_1, 10) +
       parseFloat(input_value_2, 10)
   );
}
}

Examples

## Not run: 
app$callback(
  output('output-clientside', 'children'),
  params=list(input('input', 'value')),
  clientsideFunction(
  namespace = 'my_clientside_library',
  function_name = 'my_function'
  )
)

# Passing JavaScript as a character string
app$callback(
 output('output-clientside', 'children'),
 params=list(input('input', 'value')),
 "function (value) {
       return 'Client says \"' + value + '\"';
 }"
)
## End(Not run)

dash documentation built on June 23, 2022, 9:11 a.m.