双击R-shiny

cha*_*asi 6 r rstudio shiny

我有一个小问题.我试过很多研究,但我没有运气.有没有一种方法R-shiny必须捕捉像按钮这样的元素的双击.

Ram*_*ath 14

这是一种方法.关键是dblclick在客户端检测事件(即ui),然后调用Shiny.onInputChange以更新R变量的值,然后服务器可以获取该变量的值.

这是双击按钮时发生的情况.

  1. 该值增加1
  2. 递增的值用于更新变量x.
  3. 服务器检测到更改 x
  4. 服务器更新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))
Run Code Online (Sandbox Code Playgroud)