[英]Double Click in R-shiny
我有一个小问题。 我试过很多研究,但我没有运气。 有没有一种方法R-shiny必须捕捉像按钮这样的元素的双击。
这是一种方法。 关键是要检测dblclick
在客户端侧(即,UI)事件,然后调用Shiny.onInputChange
更新的R可变,其然后可以通过服务器被拾起的值。
这是双击按钮时发生的情况。
x
。 x
变化 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))
我根据下面的评论更新了我的答案。 在这里,我使用0.2秒的时间差阈值来区分双时钟和常规点击。 我在My App中使用了稍微不同的方法。 我只是检查按钮被按下了多少次,检查它是否可以被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.