8

I have an HTML button in Shiny that when clicked, calls the JavaScript function geocodeAddressStreet(...) in a .js file in the /www directory:

tags$body(tags$input(type = "button", 
                     value = "Next", 
                     id = "button1", 
                     onClick = "geocodeAddressStreet(houseNumber,streetName,addressCity,addressState)"))),

However I cannot figure out how to get this button to call a second file when clicked, let's say foo.R. Both procedures run independently, but it seems like there's no way to add an inputId to an HTML element in Shiny. We currently have two different buttons, one of which calls R code and the other of which calls JavaScript code, but this is clearly an impractical solution.

5
  • In server.r can't you just set up an observe() that will call the script when the button is clicked?
    – John Paul
    Jul 10, 2015 at 17:24
  • Don't you need an inputId though? This button just has a standard HTML ID as far as I can tell. Jul 10, 2015 at 17:35
  • What's wrong with the standard actionButton shiny function?
    – nicola
    Jul 10, 2015 at 17:36
  • We couldn't get it to call the JavaScript code we needed, which is in a different file. Jul 10, 2015 at 17:37
  • 3
    actionButton("name","This is a Button",onClick="geoCodeAddressStreet")
    – Mark
    Aug 12, 2015 at 16:01

1 Answer 1

7

So you want to have a button and when you click it, both a javascript function and some R code get called? I was able to do this with the onclick function from the shinyjs package (disclaimer: I wrote the package)

library(shinyjs)

jsCode <- "
shinyjs.geocodeAddr = function(params) {
  alert('JavaScript called!');
  // geocodeAddressStreet(...)
}
"

runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    extendShinyjs(text = jsCode),
    actionButton("btn", "Click me")
  ),
  server = function(input, output, session) {
    onclick("btn", {
      js$geocodeAddr()
      cat("R called as well")
    })
  }
))

Basically, the onclick function is an R function that will run when the button is clicked. Within it you can obviously call R code easily, but you can also call JS code using the shinyjs package -- that's how I made a call to js$geocodeAddr. Look at the extendShinyjs function. Alternatively, instead of using extendShinyjs(), you can also use the usual approach of session$sendCustomMessage(...)

2
  • Thanks! I'm about to try this. How can I do this while keeping the JavaScript in a separate file though? Like instead of jsCode <- "(javaScript code)", so jsCode <- load(blah.js) Jul 10, 2015 at 19:05
  • extendShinyjs(script = "path/to/js") instead of the text parameter. Hence my suggestion "Look at the extendShinyjs function -- read its documentation, it'll take 5 minutes, but might make your life easier :)(hopefully)
    – DeanAttali
    Jul 10, 2015 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.