clientsideFunction: Define a clientside callback

Description Usage Arguments Details Examples

View source: R/utils.R

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

1
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:

1
2
3
4
5
6
7
8
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## 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 Nov. 7, 2021, 9:06 a.m.