本文介绍了R Shiny-是否启用键盘快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法像功能键F1-F10那样显示键盘按键来控制发亮,例如切换标签?

Is there a way to expose keyboard presses like function keys F1-F10 to control shiny, e.g. switching tabs?

推荐答案

我能够提出一种半工作解决方案,但是Shiny确实有一些局限性,所以我打开了Shiny的bug.

I was able to come up with a semi-working solution, but shiny does have some limitations so I opened a bug with shiny.

代码如下:

library(shiny)

jscode <- "
$(function(){
  $(document).keyup(function(e) {
    if (e.which >= 49 && e.which <= 57) {
      Shiny.onInputChange('numpress', e.which - 48);
    }
  });
})
"

runApp(shinyApp(
  ui = fluidPage(
    tags$script(HTML(jscode)),
    "Type a number to switch to that tab",
    tabsetPanel(
      id = "navbar",
      tabPanel("tab1", "Tab 1"),
      tabPanel("tab2", "Tab 2"),
      tabPanel("tab3", "Tab 3"),
      tabPanel("tab4", "Tab 4")
    )
  ),
  server = function(input, output, session) {
    observe({
      if (is.null(input$numpress)) {
        return()
      }
      updateTabsetPanel(session, "navbar", sprintf("tab%s", input$numpress))
    })
  }
))

这是描述问题的闪亮问题的链接: https://github.com/rstudio/shiny/issues/928

And here's the link to the shiny issue describing the problem: https://github.com/rstudio/shiny/issues/928

这篇关于R Shiny-是否启用键盘快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:45