add_js: Adds a custom JS function

Description Usage Arguments Author(s) Examples

View source: R/js.R

Description

Adds the given JS function definition to the current shiny web app.

Usage

1
2
3
add_js(type, function_text)

run_js(type, ...)

Arguments

type

name of the function

function_text

JS function definition, this should be an anonymous function accepting exactly one argument

...

arguments to pass to the function, those will be combined to a list and end up as an array in JS, unnamed parameters will be available via params[0..], named parameters can also be used with params.name

Author(s)

richard.kunze

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## Only run examples in interactive R sessions
if (interactive()) {

library(shiny)
shinyApp(
  ui = fluidPage(
    add_js(type="addValues", "function(params) {
      alert('Result is: ' + (parseInt(params[0]) + parseInt(params[1])));
    }"),
    add_js(type="myName", "function(params) {
      alert('My name is ' + params.name);
    }"),
    actionButton("btn1", "Add Values"),
    actionButton("btn2", "What's my name?")
  ),
  server = function(input, output) {
    observeEvent(input$btn1, run_js(type = "addValues", 17, 25))
    observeEvent(input$btn2, run_js(type = "myName", name = "Paul"))
  }
)

}

daqana/dqshiny documentation built on Sept. 1, 2020, 4:31 p.m.