Double-click in R-shiny

I had some small questions. I tried to research this a lot, but I had no luck. Is there a way that R-shiny should double-click on an element similar to a button.

+6
source share
2 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 call Shiny.onInputChange to update the value of the R variable, which can then be selected by the server.

This is what happens when the button is pressed twice.

  • The value is increased by 1
  • The increment is used to update the variable x .
  • Server detects changes in x
  • The server is updating 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)) 
+12
source

I updated my answer based on the comment below. Here I used a 0.2 second time difference threshold to distinguish between a double clock and a normal click. In my application, I used a slightly different approach. I just check how many times the button is pressed, checking if it is divisible by 2 or not.

 library(shiny) t1 <<- Sys.time() ui =fluidPage( actionButton("my_button", "Dont Touch it!"), mainPanel(textOutput("x")) ) server = function(input, output, session){ my_data <- reactive({ if(input$my_button == 0) { return() } if(input$my_button%%2!=0) { t1 <<- Sys.time() } if(input$my_button%%2==0 & (Sys.time() - t1 <= 0.2)) { "You pushed the button twice!" } }) output$x <- renderText({my_data()}) } runApp(list(ui = ui, server = server)) 
0
source

Source: https://habr.com/ru/post/976281/


All Articles