| Title: | Use Browser Focus Events in 'shiny' |
|---|---|
| Description: | As a user moves through a 'shiny' app, it can be useful to trigger server events when different parts of the app gain or lose focus. This package aims to make it easy to implement such events. |
| Authors: | Jon Harmon [aut, cre] (ORCID: <https://orcid.org/0000-0003-4781-4346>) |
| Maintainer: | Jon Harmon <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9000 |
| Built: | 2026-06-01 07:28:17 UTC |
| Source: | https://github.com/shinyworks/shinyfocus |
Set up a shiny::observeEvent() observer to trigger when the named input
loses focus.
on_blur( id, handler_expr, ..., priority = 99999, session = shiny::getDefaultReactiveDomain() )on_blur( id, handler_expr, ..., priority = 99999, session = shiny::getDefaultReactiveDomain() )
id |
The ID string of an input. |
handler_expr |
The expression to trigger whenever the specified input loses focus. This expression is quoted and executed in the calling environment. |
... |
Arguments passed on to
|
priority |
An integer that controls the priority with which the observer should be executed. It often makes sense for this priority to be very high to avoid conflicts. |
session |
The session (aka domain) in which the observer will be created and executed. The default is almost always desired. |
A shiny observer (see shiny::observe()).
Other observers:
on_focus_change(),
on_focus()
## Only run examples in interactive R sessions if (interactive()) { # App 1: A relatively simple ui without modules. shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), shiny::textInput("input1", "Input 1"), shiny::textInput("input2", "Input 2"), shiny::actionButton("go_button", "Go!"), shiny::textOutput("blurring") ), server = function(input, output, session) { # Update the value in blurring whenever input1 loses focus. on_blur( "input1", output$blurring <- shiny::renderText( paste( "You left input1 at", Sys.time() ) ) ) } ) # App 2: With module. blurUI <- function(id) { shiny::tagList( shiny::textInput(shiny::NS(id, "observe_me"), "Observe me"), shiny::textOutput(NS(id, "blurring")) ) } blurServer <- function(id) { shiny::moduleServer(id, function(input, output, session) { on_blur( "observe_me", output$blurring <- shiny::renderText( paste( "You left observe_me at", Sys.time() ) ) ) }) } shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), blurUI("blur_module"), shiny::textInput("another_input", "Another input"), shiny::actionButton("go_button", "Go!") ), server = function(input, output, session) { blurServer("blur_module") } ) }## Only run examples in interactive R sessions if (interactive()) { # App 1: A relatively simple ui without modules. shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), shiny::textInput("input1", "Input 1"), shiny::textInput("input2", "Input 2"), shiny::actionButton("go_button", "Go!"), shiny::textOutput("blurring") ), server = function(input, output, session) { # Update the value in blurring whenever input1 loses focus. on_blur( "input1", output$blurring <- shiny::renderText( paste( "You left input1 at", Sys.time() ) ) ) } ) # App 2: With module. blurUI <- function(id) { shiny::tagList( shiny::textInput(shiny::NS(id, "observe_me"), "Observe me"), shiny::textOutput(NS(id, "blurring")) ) } blurServer <- function(id) { shiny::moduleServer(id, function(input, output, session) { on_blur( "observe_me", output$blurring <- shiny::renderText( paste( "You left observe_me at", Sys.time() ) ) ) }) } shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), blurUI("blur_module"), shiny::textInput("another_input", "Another input"), shiny::actionButton("go_button", "Go!") ), server = function(input, output, session) { blurServer("blur_module") } ) }
Set up a shiny::observeEvent() observer to trigger when the named input
gains focus.
on_focus( id, handler_expr, ..., priority = 99999, session = shiny::getDefaultReactiveDomain() )on_focus( id, handler_expr, ..., priority = 99999, session = shiny::getDefaultReactiveDomain() )
id |
The ID string of an input. |
handler_expr |
The expression to trigger whenever the specified input gains focus. This expression is quoted and executed in the calling environment. |
... |
Arguments passed on to
|
priority |
An integer that controls the priority with which the observer should be executed. It often makes sense for this priority to be very high to avoid conflicts. |
session |
The session (aka domain) in which the observer will be created and executed. The default is almost always desired. |
A shiny observer (see shiny::observe()).
Other observers:
on_blur(),
on_focus_change()
## Only run examples in interactive R sessions if (interactive()) { # App 1: A relatively simple ui without modules. shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), shiny::textInput("input1", "Input 1"), shiny::textInput("input2", "Input 2"), shiny::actionButton("go_button", "Go!"), shiny::textOutput("focusing") ), server = function(input, output, session) { # Update the value in focusing whenever input1 has focus. on_focus( "input1", output$focusing <- shiny::renderText( paste( "You entered input1 at", Sys.time() ) ) ) } ) # App 2: With module. focusUI <- function(id) { shiny::tagList( shiny::textInput(shiny::NS(id, "observe_me"), "Observe me"), shiny::textOutput(NS(id, "focusing")) ) } focusServer <- function(id) { shiny::moduleServer(id, function(input, output, session) { on_focus( "observe_me", output$focusing <- shiny::renderText( paste( "You entered observe_me at", Sys.time() ) ) ) }) } shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), focusUI("focus_module"), shiny::textInput("another_input", "Another input"), shiny::actionButton("go_button", "Go!") ), server = function(input, output, session) { focusServer("focus_module") } ) }## Only run examples in interactive R sessions if (interactive()) { # App 1: A relatively simple ui without modules. shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), shiny::textInput("input1", "Input 1"), shiny::textInput("input2", "Input 2"), shiny::actionButton("go_button", "Go!"), shiny::textOutput("focusing") ), server = function(input, output, session) { # Update the value in focusing whenever input1 has focus. on_focus( "input1", output$focusing <- shiny::renderText( paste( "You entered input1 at", Sys.time() ) ) ) } ) # App 2: With module. focusUI <- function(id) { shiny::tagList( shiny::textInput(shiny::NS(id, "observe_me"), "Observe me"), shiny::textOutput(NS(id, "focusing")) ) } focusServer <- function(id) { shiny::moduleServer(id, function(input, output, session) { on_focus( "observe_me", output$focusing <- shiny::renderText( paste( "You entered observe_me at", Sys.time() ) ) ) }) } shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), focusUI("focus_module"), shiny::textInput("another_input", "Another input"), shiny::actionButton("go_button", "Go!") ), server = function(input, output, session) { focusServer("focus_module") } ) }
Set up a shiny::observeEvent() observer to trigger when the named input
gains or loses focus.
on_focus_change( id, handler_expr, ..., change_on = c("focus", "blur"), priority = 99999, session = shiny::getDefaultReactiveDomain() )on_focus_change( id, handler_expr, ..., change_on = c("focus", "blur"), priority = 99999, session = shiny::getDefaultReactiveDomain() )
id |
The ID string of an input. |
handler_expr |
The expression to trigger whenever the specified input changes focus. This expression is quoted and executed in the calling environment. |
... |
Arguments passed on to
|
change_on |
A character indicating whether the observer should update when the input becomes focused and/or when the input becomes blurred. |
priority |
An integer that controls the priority with which the observer should be executed. It often makes sense for this priority to be very high to avoid conflicts. |
session |
The session (aka domain) in which the observer will be created and executed. The default is almost always desired. |
A shiny observer (see shiny::observe()).
Other observers:
on_blur(),
on_focus()
if (interactive()) { # App 1: A relatively simple ui without modules. shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), shiny::textInput("input1", "Input 1"), shiny::textInput("input2", "Input 2"), shiny::actionButton("go_button", "Go!"), shiny::textOutput("changing") ), server = function(input, output, session) { # Update the value in "changing" whenever input1 gains or loses focus. on_focus_change( "input1", output$changing <- shiny::renderText( paste( "You entered or left input1 at", Sys.time() ) ) ) } ) # App 2: With module. changeUI <- function(id) { shiny::tagList( shiny::textInput(shiny::NS(id, "observe_me"), "Observe me"), shiny::textOutput(NS(id, "changing")) ) } changeServer <- function(id) { shiny::moduleServer(id, function(input, output, session) { on_focus_change( "observe_me", output$changing <- shiny::renderText( paste( "You entered or left observe_me at", Sys.time() ) ) ) }) } shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), changeUI("change_module"), shiny::textInput("another_input", "Another input"), shiny::actionButton("go_button", "Go!") ), server = function(input, output, session) { changeServer("change_module") } ) }if (interactive()) { # App 1: A relatively simple ui without modules. shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), shiny::textInput("input1", "Input 1"), shiny::textInput("input2", "Input 2"), shiny::actionButton("go_button", "Go!"), shiny::textOutput("changing") ), server = function(input, output, session) { # Update the value in "changing" whenever input1 gains or loses focus. on_focus_change( "input1", output$changing <- shiny::renderText( paste( "You entered or left input1 at", Sys.time() ) ) ) } ) # App 2: With module. changeUI <- function(id) { shiny::tagList( shiny::textInput(shiny::NS(id, "observe_me"), "Observe me"), shiny::textOutput(NS(id, "changing")) ) } changeServer <- function(id) { shiny::moduleServer(id, function(input, output, session) { on_focus_change( "observe_me", output$changing <- shiny::renderText( paste( "You entered or left observe_me at", Sys.time() ) ) ) }) } shiny::shinyApp( ui = shiny::fluidPage( shinyfocus_js_dependency(), changeUI("change_module"), shiny::textInput("another_input", "Another input"), shiny::actionButton("go_button", "Go!") ), server = function(input, output, session) { changeServer("change_module") } ) }
Add the shinyfocus.js script to a shiny app exactly once. Adding this
script setups up the object used to detect changes in focus.
shinyfocus_js_dependency()shinyfocus_js_dependency()
An htmltools::htmlDependency(), which shiny uses to add the
shinyfocus.js script exactly once.
shinyfocus_js_dependency()shinyfocus_js_dependency()