Use JQuery to trigger a button click that procs observeEvent

55 views
Skip to first unread message

Bryan Britten

unread,
Jan 13, 2016, 8:43:24 PM1/13/16
to Shiny - Web Framework for R
Let's say that I have two actionButtons in my app that are left and right arrows. They're used to navigate through multiple data tables and plots and given an id of "left" and "right". What I'd like is to be able to navigate with the arrow keys on my keyboard as well. I thought I'd be able to use JQuery to simply trigger a click event on those buttons, but that doesn't appear to get communicated to Shiny.

So, is there a different way for me to do this? Here's the JQuery I'm currently using:

$(document.ready(function() {
    $("body").keydown(function(e) {
        if (e.which === 37) {
            $("#plot-left").trigger("click");
        } else if (e.which === 39) {
            $("#plot-right").trigger("click");
        }
     });
});

On Shiny's side, I've got this code:

  increase_counter <- function() {
    values$counter <- values$counter + 1
    if (values$counter > values$cluster_count) values$counter <- 1
  }
 
  decrease_counter <- function() {
    values$counter <- values$counter - 1
    if (values$counter == 0) values$counter <- values$cluster_count
  }

  observeEvent(input$plot_left, {
    decrease_counter()
  })
 
  observeEvent(input$plot_right, {
    increase_counter()
  })

This isn't a crucial feature, but it'd be nice to accomplish. Thanks!

Dean Attali

unread,
Jan 13, 2016, 9:12:29 PM1/13/16
to Shiny - Web Framework for R
I answered on Stackoverflow
Reply all
Reply to author
Forward
0 new messages