Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method for updating the total on the fly #129

Closed
SangdonLim opened this issue Dec 1, 2023 · 2 comments
Closed

Add a method for updating the total on the fly #129

SangdonLim opened this issue Dec 1, 2023 · 2 comments

Comments

@SangdonLim
Copy link

SangdonLim commented Dec 1, 2023

Description

I thought it would be useful to add a method for updating the total on the fly.

But Why?

Here is an example use case:

already_processed <- rep(FALSE, 100)

pb <- progress_bar$new(
  format = "[:bar] :current / :total :eta",
  clear = FALSE, total = 100
)

for (i in 1:100) {
  
  if (already_processed[i]) {
    pb$tick(1)
    next
  }
  
  Sys.sleep(1)
  already_processed[i] <- TRUE
  pb$tick(1)
  
}

This is a progress bar for 100 tasks, with each task taking a long time. It is set to one second here but it can be hours in real use. I'm using this to get an ETA.
When the code is run from scratch, it gives an accurate ETA.
However, when I break the loop midwhile because I need to pause it to take a look at it or do something else, say at i = 20, once I restart the code, it will give an inaccurate ETA because it instantaneously skipped the first already processed 20 tasks, throwing the prediction off.

As a workaround I am currently doing the below:

already_processed <- rep(FALSE, 100)

pb <- progress_bar$new(
  format = "[:bar] :current / :total :eta",
  clear = FALSE, total = 100
)

for (i in 1:100) {
  
  if (already_processed[i]) {
    pb$.__enclos_env__$private$total <-
    pb$.__enclos_env__$private$total - 1
    pb$tick(0)
    next
  }
  
  Sys.sleep(1)
  already_processed[i] <- TRUE
  pb$tick(1)
  
}

which decreases the total for every already completed task. This gives an accurate ETA in the above scenario.
But pb$.__enclos_env__$private$total <- pb$.__enclos_env__$private$total - 1 is just.. ugly, so I propose making this into something like pb$change_total_by(-1). Or more properly pb$tick(1, exclude_from_eta = TRUE) could be nice.

Of course, I could do this in a way that sets the right amount of total at the time pb is first initialized. This is doable in this scenario but may not be always doable in every scenario.

@gaborcsardi
Copy link
Member

gaborcsardi commented Dec 1, 2023

I believe this is already with the progress bars in the cli package, which we are focusing on currently, so it is unlikely that we'll implement it in the progress package.

See https://cli.r-lib.org/reference/index.html#progress-bars

@SangdonLim
Copy link
Author

@gaborcsardi Good to know! I didn't know about the cli package. I've just tried it and it fits my needs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants