With a JavaScript handler, once you have calculated a value based on the handler's arguments (e.g. name, value) you will likely want to produce a side-effect based on that calculated value. This function helps you do that.

vw_handler_add_effect(vw_handler, body_effect, ...)

Arguments

vw_handler

vw_handler created using vw_handler_signal() or vw_handler_event()

body_effect

character, the name of a defined handler-body, or the text of the body of a handler-function

...

additional named parameters to be interpolated into the text of the handler_body

Value

modified copy of vw_handler

Details

The calculation of a value is meant to be separate from the production of a side-effect. This way, the code for a side-effect can be used for any type of handler.

You are supplying the body_effect to an effect-handler. This takes a single argument, x, representing the calculated value. Doing this allows us to chain side-effects together; be careful not to modify x in any of the code you provide.

To see what side-effects are available in this package's handler-library, call vw_handler_add_effect() without any arguments. You may notice that some of the effects, like "element_text", require additional parameters, in this case, selector.

Those parameters with a default value of NULL require you to supply a value; those with sensible defaults are optional.

To provide the parameters, call vw_handler_add_effect() with named arguments corresponding to the names of the parameters. See the examples for details.

Examples

  # list all the available effect-handlers
  vw_handler_add_effect()
#> arguments: x 
#> 
#>   body_effect: shiny_input 
#>     params: inputId = NULL, expr = "x" 
#> 
#>     // sets the Shiny-input named `inputId` to `expr` (default "x")
#>     Shiny.setInputValue('${inputId}', ${expr});
#> 
#>   body_effect: console 
#>     params: label = "", expr = "x" 
#> 
#>     // if `label` is non-trivial, prints it to the JS console
#>     '${label}'.length > 0 ? console.log('${label}') : null;
#>     // prints `expr` (default "x") to the JS console
#>     console.log(${expr});
#> 
#>   body_effect: element_text 
#>     params: selector = NULL, expr = "x" 
#> 
#>     // to element `selector`, adds text `expr` (default "x")
#>     document.querySelector('${selector}').innerText = ${expr}

  # build a signal handler that prints some text,
  # then the value, to the console
  vw_handler_signal("value") %>%
    vw_handler_add_effect("console", label = "signal value:")
#> arguments: name, value 
#> 
#> body_value:
#>   // returns the value of the signal
#>   return value;
#> body_effect:
#>   // if `label` is non-trivial, prints it to the JS console
#>   'signal value:'.length > 0 ? console.log('signal value:') : null;
#>   // prints `expr` (default "x") to the JS console
#>   console.log(x);