R shiny - enabling keyboard shortcuts?

可紊 提交于 2019-12-21 20:28:34

问题


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


回答1:


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

Here's the code:

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))
    })
  }
))

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



来源:https://stackoverflow.com/questions/32002170/r-shiny-enabling-keyboard-shortcuts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!