Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Click in R-shiny

Tags:

r

rstudio

shiny

I had a small questions. I have tried researching this a lot but I have had no luck. Is there a way R-shiny has to capture a double click on an element like a button.

like image 396
chaitanya.varanasi Avatar asked Oct 06 '14 00:10

chaitanya.varanasi


1 Answers

Here is one way to do it. The key is to detect the dblclick event on the client side (i.e. ui), and then invoke Shiny.onInputChange to update the value of an R variable, which can then be picked up by the server.

Here is what happens when the button is double clicked.

  1. The value is incremented by 1
  2. The incremented value is used to update the variable x.
  3. The server detects change in x
  4. The server updates the textOutput.
library(shiny)

ui = bootstrapPage(
  tags$button(id = 'mybutton', 'button', class='btn btn-primary', value = 0),
  textOutput('x'),
  # when button is double clicked increase the value by one
  # and update the input variable x
  tags$script("
    $('#mybutton').on('dblclick', function(){
      var val = +this.value
      this.value = val + 1
      Shiny.onInputChange('x', this.value)
      console.log(this.value)
    })  
  ")
)

server = function(input, output, session){
  output$x <- renderText({
    input$x
  })
}

runApp(list(ui = ui, server = server))
like image 127
Ramnath Avatar answered Oct 04 '22 01:10

Ramnath