diff --git a/DESCRIPTION b/DESCRIPTION index b42fa86a..10feb225 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,6 +40,7 @@ Suggests: rmarkdown, rprojroot, rstudioapi, + shiny, testthat (>= 3.2.0), tibble, whoami, diff --git a/NEWS.md b/NEWS.md index 170ac4d2..d9ee0730 100644 --- a/NEWS.md +++ b/NEWS.md @@ -51,6 +51,9 @@ error when the progress bar is drawn with a value below zero or above the total (#580, @taekop). +* `cli_progress_output()` no longer fails with the Shiny progress handler + if the progress bar has not been shown yet (#762, @taekop). + # cli 3.6.6 * New `{.num}` and `{.bytes}` inline styles to format numbers diff --git a/R/progress-server.R b/R/progress-server.R index b2b287bd..7846305c 100644 --- a/R/progress-server.R +++ b/R/progress-server.R @@ -392,10 +392,12 @@ builtin_handler_shiny <- list( output = function(bar, .envir, text) { bar$shiny_output <- last_lines(paste0(bar$shiny_output, " \u2022 ", text)) - bar$shiny_progress$set( - value = bar$current, - detail = shiny_detail(bar, .envir) - ) + if (!is.null(bar$shiny_progress)) { + bar$shiny_progress$set( + value = bar$current, + detail = shiny_detail(bar, .envir) + ) + } } ) diff --git a/tests/testthat/test-progress-handlers.R b/tests/testthat/test-progress-handlers.R index 1f094494..81cd1e4b 100644 --- a/tests/testthat/test-progress-handlers.R +++ b/tests/testthat/test-progress-handlers.R @@ -56,3 +56,18 @@ test_that("builtin_handlers", { expect_true(is.list(builtin_handlers())) expect_true(all(c("cli", "shiny", "rstudio") %in% names(builtin_handlers()))) }) + +test_that("shiny handler, cli_progress_output() before the bar is shown", { + skip_if_not_installed("shiny") + withr::local_options( + cli.progress_handlers_only = "shiny", + cli.progress_show_after = 3600 + ) + server <- function(input, output, session) { + id <- cli_progress_bar(total = 10) + cli_progress_output("custom message", id = id) + cli_progress_update(id = id) + cli_progress_done(id = id) + } + expect_no_error(shiny::testServer(server, {})) +})