我有一个小问题。我试过很多研究,但我没有运气。有没有一种方法R-shiny必须捕捉像按钮这样的元素的双击。
答案 0 :(得分:13)
这是一种方法。关键是检测客户端的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))
答案 1 :(得分:0)
我已根据以下评论更新了我的答案。在这里,我使用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))