Is there a way to expose keyboard presses like function keys F1-F10 to control shiny, e.g. switching tabs?
Asked
Active
Viewed 2,180 times
1 Answers
6
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

DeanAttali
- 25,268
- 10
- 92
- 118
-
thanks! Looks like this isn't well addressed by shiny in its current form. Looking at the issue request ... if the functional aspect in FRP is being respected, the primary reason for keyboard shortcuts would be to control views (like switching tabs) rather than triggering reactive flows. However, I can see how sometimes what defines a view can start to blur (for example a selectInput to select a dataset to display). – daj Aug 30 '15 at 12:58
-
Do you know if there has been any progress on this? – InspectorSands Jan 16 '17 at 10:58
-
There hasn't been any work on this feature yet. The shiny team is very small and they've been extremely busy with other big features and bug fixes and haven't had time yet – DeanAttali Jan 16 '17 at 15:52
-
Thanks, @daattali. I opened a related question to see if reactive flows could also be triggered with keyboard shortcuts. I tried adapting your code for this, to no avail: http://stackoverflow.com/questions/41675059/keyboard-shortcuts-to-trigger-reactive-flows-in-r-shiny – InspectorSands Jan 19 '17 at 11:12
-
Since then it seems that `Shiny.setInputValue()` could be used to properly does that, would it be possible to have an update of this answer using that function? – yeahman269 Jul 02 '21 at 09:34