Skip to content

Commit 6fe26e8

Browse files
Add argument 'map' to withProgressShiny() [#109]
1 parent bf5010a commit 6fe26e8

File tree

5 files changed

+81
-10
lines changed

5 files changed

+81
-10
lines changed

R/handler_shiny.R

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#'
55
#' @inheritParams make_progression_handler
66
#'
7+
#' @param map Specifies whether the progression message should be mapped
8+
#' to the 'message' and 'detail' element in the Shiny progress panel.
9+
#' This argument should be a named character string with value `"message"`
10+
#' or `"detail"` and where the name should be `message`, e.g.
11+
#' `map = c(message = "message")` or `map = c(message = "detail")`.
12+
#'
713
#' @param \ldots Additional arguments passed to [make_progression_handler()].
814
#'
915
#' @examples
@@ -21,12 +27,28 @@
2127
#'
2228
#' @keywords internal
2329
#' @export
24-
handler_shiny <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), target = "gui", ...) {
30+
handler_shiny <- function(intrusiveness = getOption("progressr.intrusiveness.gui", 1), target = "gui", map = c(message = "message"), ...) {
31+
stop_if_not(
32+
is.character(map), all(map %in% c("message", "detail")),
33+
!is.null(names(map)), all(names(map) %in% c("message"))
34+
)
35+
36+
## Default: The progression message updates Shiny 'message'
37+
map_args <- function(state) list(message = state$message)
38+
39+
## Should progress message update another Shiny field?
40+
if ("message" %in% names(map)) {
41+
if (map["message"] == "detail") {
42+
map_args <- function(state) list(detail = state$message)
43+
}
44+
}
45+
2546
reporter <- local({
2647
list(
2748
update = function(config, state, progression, ...) {
2849
amount <- if (config$max_steps == 0) 1 else progression$amount / config$max_steps
29-
shiny::incProgress(amount = amount, message = state$message)
50+
args <- c(list(amount = amount), map_args(state))
51+
do.call(shiny::incProgress, args = args)
3052
}
3153
)
3254
})

R/withProgressShiny.R

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
#' Use Progressr in Shiny Apps: Plug-in Backward Compatibility Replacement for shiny::withProgress()
22
#'
3+
#' @inheritParams handler_shiny
4+
#'
35
#' @param expr,\ldots,env,quoted Arguments passed to [shiny::withProgress] as is.
46
#'
7+
#' @param message,detail (character string) The message and the detail message to be passed to [shiny::withProgress()].
8+
#'
59
#' @param handlers Zero or more progression handlers used to report on progress.
610
#'
711
#' @return The value of [shiny::withProgress].
812
#'
913
#' @example incl/withProgressShiny.R
1014
#'
1115
#' @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.
1318
#'
1419
#' @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))) {
1621
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+
1743
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))
1945
if (res$visible) res$value else invisible(res$value)
2046
}

incl/withProgressShiny.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ app <- shinyApp(
1010
output$plot <- renderPlot({
1111
X <- 1:15
1212
withProgressShiny(message = "Calculation in progress",
13-
detail = "This may take a while ...", value = 0, {
13+
detail = "Starting ...",
14+
map = c(message = "detail"),
15+
value = 0, {
1416
p <- progressor(along = X)
1517
y <- lapply(X, FUN=function(x) {
1618
Sys.sleep(0.25)
17-
p()
19+
p(sprintf("x=%d", x))
1820
})
1921
})
2022

man/handler_shiny.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/withProgressShiny.Rd

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)