Skip to content

Call Javascript from R #277

Answered by kamilzyla
dmenne asked this question in Q&A
May 19, 2022 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

Thanks for @dmenne for clarifying! To use shinyjs::extendShinyjs() you can do something like this:

app/js/index.js

function sayHello(name) {
  alert(`Hello ${name}!`);
}

function sayBye(name) {
  alert(`Bye ${name}!`);
}

export const extensions = { sayHello, sayBye };

Remember to run rhino::build_js() whenever you edit index.js.

app/main.R

box::use(
  shiny[actionButton, fluidPage, moduleServer, NS, observeEvent],
  shinyjs[extendShinyjs, js, useShinyjs],
)


#' @export
ui <- function(id) {
  ns <- NS(id)
  fluidPage(
    useShinyjs(),
    extendShinyjs(
      text = "shinyjs = { ...shinyjs, ...App.extensions }",
      functions = c("sayHello", "sayBye")
    ),
    actionButton(ns("hello"

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@dmenne
Comment options

@dmenne
Comment options

@dmenne
Comment options

@dmenne
Comment options

Comment options

You must be logged in to vote
1 reply
@dmenne
Comment options

Answer selected by dmenne
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants