【发布时间】:2025-11-25 20:05:02
【问题描述】:
我有一个小问题。我已经尝试了很多研究,但我没有运气。有没有一种方法 R-shiny 必须捕获对按钮等元素的双击。
【问题讨论】:
我有一个小问题。我已经尝试了很多研究,但我没有运气。有没有一种方法 R-shiny 必须捕获对按钮等元素的双击。
【问题讨论】:
这是一种方法。关键是在客户端(即ui)检测dblclick事件,然后调用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))
【讨论】: