简体   繁体   English

双击R-shiny

[英]Double Click in R-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. 有没有一种方法R-shiny必须捕捉像按钮这样的元素的双击。

Here is one way to do it. 这是一种方法。 The key is to detect the dblclick event on the client side (ie ui), and then invoke Shiny.onInputChange to update the value of an R variable, which can then be picked up by the server. 关键是要检测dblclick在客户端侧(即,UI)事件,然后调用Shiny.onInputChange更新的R可变,其然后可以通过服务器被拾起的值。

Here is what happens when the button is double clicked. 这是双击按钮时发生的情况。

  1. The value is incremented by 1 该值增加1
  2. The incremented value is used to update the variable x . 递增的值用于更新变量x
  3. The server detects change in x 服务器检测到x变化
  4. The server updates the textOutput . 服务器更新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)) 

I've updated my answer based on the comment below. 我根据下面的评论更新了我的答案。 Here I used a threshold of time difference of 0.2 seconds to differentiate between a double clock and a regular click. 在这里,我使用0.2秒的时间差阈值来区分双时钟和常规点击。 I used slightly different approach in My App. 我在My App中使用了稍微不同的方法。 I simply check how many times the button has been pressed by checking if its divisible by 2 or not. 我只是检查按钮被按下了多少次,检查它是否可以被2整除。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM