HenrikBengtsson / progressr

三 R package: An Inclusive, Unifying API for Progress Updates
https://progressr.futureverse.org
280 stars 12 forks source link

Progress bar in RStudio Background Job #174

Open dominicroye opened 1 week ago

dominicroye commented 1 week ago

I use a progress bar for my function in future_map() running as a background job in RStudio. It works with enable = TRUE, but is it possible to avoid repeating the bar each update?

  progressr::handlers("cli")
  progressr::with_progress({
    p <- progressr::progressor(steps = mod_len)
    furrr::future_map(1:mod_len, .function_, p = p)

  }, enable = TRUE)

image

HenrikBengtsson commented 1 week ago

Thanks for reporting. First, note that it works with the other terminal progress handlers, e.g. handlers("progress") and handlers("txtprogressbar").

So, this might be an issue with the cli package, because you get the same multi-line output when we use on the cli package;

library(cli)
clean <- function() {
  cli_progress_bar("Cleaning data", total = 100)
  for (i in 1:100) {
    Sys.sleep(5/100)
    cli_progress_update()
  }
  cli_progress_done()
}
clean()

Could you please check with them over at https://github.com/r-lib/cli/issues?

gaborcsardi commented 1 week ago

FWIW, this does not work either:

Sys.sleep(1)
cat("foo")
Sys.sleep(1)
cat("bar")
Sys.sleep(1)
cat("\rerase")
Sys.sleep(1)
cat("\n")

so seemingly this is an issue with RStudio.

gaborcsardi commented 1 week ago

@dominicroye did this ever work in the job pane?

dominicroye commented 1 week ago

For me it works. I tried out RStudio Server and local installation.

RStudio 2024.09.0+368 "Cranberry Hibiscus" Release (eab7b3d81d2d6917eb79d8f6cb78b829e09f297d, 2024-09-12) for Ubuntu Jammy Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36, Quarto 1.5.57 (/opt/quarto/bin/quarto)

RStudio 2024.09.0+375 "Cranberry Hibiscus" Release (c8fc7aee6dc218d5687553f9041c6b1e5ea268ff, 2024-09-16) for windows Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2024.09.0+375 Chrome/124.0.6367.243 Electron/30.4.0 Safari/537.36, Quarto 1.5.57 (C:/Users/xeo19/AppData/Local/Programs/Quarto/bin/quarto.exe)

HenrikBengtsson commented 1 week ago

FWIW, this does not work either:

Thanks for minimal reproducible example. I can reproduce this with RStudio Server 2024.04.1-748 and the latest 2024.09.0-375, resulting in:

foo
bar
erase

in the Background Jobs panel.

So, it looks like there is a newline appended whenever output is going to the standard output (stdout). Interestingly, this does not happen when we write to the standard output (stderr); the following behaves as expected:

con <- stderr()
Sys.sleep(1)
cat("foo", file = con)
Sys.sleep(1)
cat("bar", file = con)
Sys.sleep(1)
cat("\rerase", file = con)
Sys.sleep(1)
cat("\n", file = con)

That displays:

foo

then

foobar

and lastly

eraser

as expected.

@gaborcsardi , for my understanding, does cli::cli_progress_bar() output to stdout? And, as a workaround, is there a way to tell it to output to stderr instead?

HenrikBengtsson commented 1 week ago

I've reported this issue to https://github.com/rstudio/rstudio/issues/15325.

HenrikBengtsson commented 1 week ago

I've reported this issue to rstudio/rstudio#15325.

They confirmed it's a problem with the RStudio software. So, there's nothing to be fixed in progressr (or cli).

@gaborcsardi , for my understanding, does cli::cli_progress_bar() output to stdout? And, as a workaround, is there a way to tell it to output to stderr instead?

Other than @gaborcsardi possibly being able to suggest a workaround to have the cli progress bar output to stderr, I don't think there's more to do in this issue.

@dominicroye , please note that the terminal-based progress handlers (e.g. progess and txtprogressbar) works.