R Shiny 键输入绑定 [英] R Shiny key input binding

查看:41
本文介绍了R Shiny 键输入绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Shiny 应用程序中,是否可以绑定来监听用户按下的键?

In a Shiny application, is it possible to have a binding that listens to what key a user presses down?

我对 JavaScript 不太熟悉,但我正在寻找类似的东西:

I'm not too familiar with JavaScript, but I'm looking for something like:

window.onkeydown = function (e) {
    var code = e.keyCode ? e.keyCode : e.which;
    alert(code);
};

然后在 server.R 中使用关键输入,例如:

where the key input is then to be used in server.R, e.g.:

shinyServer(function(input, output) {

  output$text <- renderText({
    paste('You have pressed the following key:', input$key)
  })

  # ...

})

推荐答案

您可以为按键添加监听器.Shiny.onInputChange 可用于将按下的键绑定到一个闪亮的变量:

You can add a listener for keypresses. The Shiny.onInputChange can be used to bind the key pressed to a shiny variable:

library(shiny)
runApp( list(ui = bootstrapPage(
  verbatimTextOutput("results"),
  tags$script('
    $(document).on("keypress", function (e) {
       Shiny.onInputChange("mydata", e.which);
    });
  ') 
)
, server = function(input, output, session) {

  output$results = renderPrint({
    input$mydata
  })
}
))

对于 keydown 事件,您可以替换:

for keydown events you can substitute:

  tags$script('
    $(document).on("keydown", function (e) {
       Shiny.onInputChange("mydata", e.which);
    });
  ') 

这篇关于R Shiny 键输入绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆