Skip to content

Instantly share code, notes, and snippets.

@jakubsob
Last active December 5, 2024 16:10
Show Gist options
  • Save jakubsob/cbbbac78cfd6359e907a8ac088f33845 to your computer and use it in GitHub Desktop.
Save jakubsob/cbbbac78cfd6359e907a8ac088f33845 to your computer and use it in GitHub Desktop.
Wait for inputs to be updated from the server, then execute a callback
#' Run callback when all inputs are initialized
#'
#' @param ids A character vector of input ids to check.
#' @param callback A function to run after all inputs are initialized.
#' @param session A Shiny session
#' @importFrom shiny reactiveVal observe getDefaultReactiveDomain
#' @importFrom purrr map reduce
#' @importFrom checkmate test_null
on_inputs_initialized <- function(ids, callback, session = getDefaultReactiveDomain()) {
initialized_ <- reactiveVal(FALSE)
initialized_observer <- observe({
flag <- ids |>
map(\(x) session$input[[x]]) |>
map(\(x) !test_null(x)) |>
reduce(`&`)
initialized_(flag)
})
observe({
if (initialized_()) {
callback()
attr(initialized_, ".impl")$freeze()
initialized_observer$destroy()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment