|
1 | 1 | #' Use Progressr in Shiny Apps: Plug-in Backward Compatibility Replacement for shiny::withProgress() |
2 | 2 | #' |
| 3 | +#' @inheritParams handler_shiny |
| 4 | +#' |
3 | 5 | #' @param expr,\ldots,env,quoted Arguments passed to [shiny::withProgress] as is. |
4 | 6 | #' |
| 7 | +#' @param message,detail (character string) The message and the detail message to be passed to [shiny::withProgress()]. |
| 8 | +#' |
5 | 9 | #' @param handlers Zero or more progression handlers used to report on progress. |
6 | 10 | #' |
7 | 11 | #' @return The value of [shiny::withProgress]. |
8 | 12 | #' |
9 | 13 | #' @example incl/withProgressShiny.R |
10 | 14 | #' |
11 | 15 | #' @section Requirements: |
12 | | -#' This function requires the \pkg{shiny} package. |
| 16 | +#' This function requires the \pkg{shiny} package and will use the |
| 17 | +#' [handler_shiny()] **progressr** handler internally to report on updates. |
13 | 18 | #' |
14 | 19 | #' @export |
15 | | -withProgressShiny <- function(expr, ..., env = parent.frame(), quoted = FALSE, handlers = c(shiny = handler_shiny, progressr::handlers(default = NULL))) { |
| 20 | +withProgressShiny <- function(expr, ..., message = NULL, detail = NULL, map = c(message = "message"), env = parent.frame(), quoted = FALSE, handlers = c(shiny = handler_shiny, progressr::handlers(default = NULL))) { |
16 | 21 | if (!quoted) expr <- substitute(expr) |
| 22 | + |
| 23 | + stop_if_not("shiny" %in% names(handlers)) |
| 24 | + if (sum(names(handlers) == "shiny") > 1) { |
| 25 | + warning("Detected a 'shiny' handler set via progressr::handlers()") |
| 26 | + } |
| 27 | + |
| 28 | + stop_if_not( |
| 29 | + is.character(map), all(map %in% c("message", "detail")), |
| 30 | + !is.null(names(map)), all(names(map) %in% c("message")) |
| 31 | + ) |
| 32 | + |
| 33 | + ## Customize the shiny 'message' target? |
| 34 | + if (is.function(handlers$shiny) && |
| 35 | + !inherits(handlers$shiny, "progression_handler")) { |
| 36 | + tweaked_handler_shiny <- handlers$shiny |
| 37 | + if (!identical(map, formals(tweaked_handler_shiny)$map)) { |
| 38 | + formals(tweaked_handler_shiny)$map <- map |
| 39 | + handlers$shiny <- tweaked_handler_shiny |
| 40 | + } |
| 41 | + } |
| 42 | + |
17 | 43 | expr <- bquote(progressr::with_progress({.(expr)}, handlers = .(handlers))) |
18 | | - res <- withVisible(shiny::withProgress(expr, ..., env = env, quoted = TRUE)) |
| 44 | + res <- withVisible(shiny::withProgress(expr, ..., message = message, detail = detail, env = env, quoted = TRUE)) |
19 | 45 | if (res$visible) res$value else invisible(res$value) |
20 | 46 | } |
0 commit comments