I'm trying to figure out if there's a way to determine in shiny whether the user is focused on a text-field or select field. My website contains various elements such as plots, tables, numerical inputs, and buttons.
Currently, I have implemented the following script:
library(shiny)
ui <- fluidPage(
tags$script('$(document).ready(function(){ $("*").focus( function(e){ Shiny.setInputValue("focusedElement", e.target.id);}); }); '),
textOutput("output1"),
textInput(inputId = "text1", label = 'Text1', value = ""),
numericInput(inputId = 'num1',label = 'Num1', value=5),
selectInput(inputId = 'select1', label='Select1',choices = c(1,2,3)),
plotOutput('plot'),
actionButton('btn','Btn'),
DT::dataTableOutput('table'),
)
server <- function(input, output, session) {
output$output1 <- renderText({
print(input$focusedElement)
input$focusedElement })
output$table<- DT::renderDataTable(iris)
output$plot<-renderPlot(plot(iris[,c(3,4)]))
}
shinyApp(ui, server)
Despite focusing on every single input and background, only the Text-Input, Numerical-Input, and Buttons seem to work. But why is this happening? (Check the console output, select1
was certainly focused at some point but never printed, along with the search bar, plot, and background.)
https://i.sstatic.net/g7FQb.png
Please feel free to suggest entirely different approaches or correct my code style.
In essence, all I want to know is whether the user is currently in a text field (such as text1
or num1
, or the table's search bar) or interacting with a button.