From 5adea1a81d477df3ee16b52d718c98e7b1af1d65 Mon Sep 17 00:00:00 2001 From: taekop Date: Mon, 5 Oct 2026 16:36:16 +0900 Subject: [PATCH] Don't fail in the shiny progress handler before the bar is shown (#762) --- DESCRIPTION | 1 + NEWS.md | 3 +++ R/progress-server.R | 10 ++++++---- tests/testthat/test-progress-handlers.R | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b42fa86a2..10feb225e 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 170ac4d2c..d9ee07309 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 b2b287bdb..7846305ce 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 1f0944947..81cd1e4be 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, {})) +})