Skip to contents

Set up a shiny::observeEvent() observer to trigger when the named input gains or loses focus.

Usage

on_focus_change(
  id,
  handler_expr,
  ...,
  change_on = c("focus", "blur"),
  priority = 99999,
  session = shiny::getDefaultReactiveDomain()
)

Arguments

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 shiny::observeEvent

label

A label for the observer or reactive, useful for debugging.

suspended

If TRUE, start the observer in a suspended state. If FALSE (the default), start in a non-suspended state.

autoDestroy

If TRUE (the default), the observer will be automatically destroyed when its domain (if any) ends.

ignoreNULL

Whether the action should be triggered (or value calculated, in the case of eventReactive) when the input event expression is NULL. See Details.

once

Whether this observeEvent should be immediately destroyed after the first time that the code in handlerExpr is run. This pattern is useful when you want to subscribe to a event that should only happen once.

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.

Value

A shiny observer (see shiny::observe()).

See also

Other observers: on_blur(), on_focus()

Examples

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