Skip to content

Call JavaScript from R directly #323

Answered by kamilzyla
shahreyar-abeer asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @shahreyar-abeer, thanks for your question! I'd suggest a solution along these lines:

app/js/index.js

export function sayHello(name) {
  console.log(`Hello ${name}!`);
}

Shiny.addCustomMessageHandler('sayHello', sayHello);

app/main.R

box::use(
  shiny,
)

say_hello <- function(name, session = shiny$getDefaultReactiveDomain()) {
  session$sendCustomMessage("sayHello", name)
}

#' @export
ui <- function(id) {
  ns <- shiny$NS(id)
  shiny$bootstrapPage(
    shiny$textInput(ns("name"), "Your Name"),
    shiny$actionButton(ns("hello"), "Say Hello!")
  )
}

#' @export
server <- function(id) {
  shiny$moduleServer(id, function(input, output, session) {
    shiny$observeEvent(input$hello, say…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@shahreyar-abeer
Comment options

@kamilzyla
Comment options

@shahreyar-abeer
Comment options

Answer selected by shahreyar-abeer
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