Last active
December 5, 2024 16:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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