Navigation Menu

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

Error overwrites current progress bar #439

Open
rpatin opened this issue Mar 17, 2022 · 4 comments
Open

Error overwrites current progress bar #439

rpatin opened this issue Mar 17, 2022 · 4 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@rpatin
Copy link

rpatin commented Mar 17, 2022

Hi all,
First thanks for your work with this package, which is really nice to use.
I don't think this is directly an issue with the package, but I could not find answers elsewhere.
When an error occurs in a script using progress bar, the error message is written over the last iteration of the progress bar, which is quite annoying. I have tried using custom functions in options(error = myfunction) but it seems it is executed after the error so it does not change anything.

Here is a reproducible example :

cli::cli_progress_step("This is a very very very very very long test message")
stop("small error")

Would you have any advice on how to solve that ?
Thanks in advance,
Rémi

@gaborcsardi
Copy link
Member

If you are throwing the error then you can clear the status bar first.

{
cli::cli_progress_step("This is a very very very very very long test message")
cli::cli_progress_done(result = "failed")
stop("small error")
}
#> ✖ This is a very very very very very long test message [5ms]
#> Error: small error

Unfortunately I could not find a nice solution if the error is thrown elsewhere. R prints the error message very eagerly, and there are only risky solutions to prevent that.

@gaborcsardi
Copy link
Member

One possible solution would be to have a helper function that adds spaces + \r (or an ANSI delete-to-line sequence) to the error message. E.g. this works in a terminal:

{
cli::cli_progress_step("This is a very very very very very long test message")
stop("\033[Ksmall error")
}
#> ✔ This is a very very very very very long test message [46.1s]
#> Error: small error

If you are inside a function, like most of the time, then you get:

local({
  cli::cli_progress_step("This is a very very very very very long test message")
  stop("\033[Ksmall error")
})
#> Error in eval(quote({ : small error
#> ✖ This is a very very very very very long test message [165ms]

@rpatin
Copy link
Author

rpatin commented Mar 17, 2022

Thanks for the solution.
This should work for error that I handle in the functions (if I throw the error).

As you said before, for errors that comes from external functions (i.e. I have wrong data in a data.frame and dplyr throws an error) this is much harder (and not recommended) to try and add this character to their error message.

@gaborcsardi gaborcsardi added the bug an unexpected problem or unintended behavior label Apr 21, 2022
@gaborcsardi
Copy link
Member

It would be probably better to print the error after the progress message, even though it will still be on the same line. That what happens e.g. with the txtProgressBar() base function:

testit <- function(x = sort(runif(20)), ...) {
  pb <- txtProgressBar(...)
  for(i in c(0, x, 1)) {
    Sys.sleep(0.5)
    setTxtProgressBar(pb, i)
    if (runif(1) < 0.2) stop("oops")
  }
  Sys.sleep(1)
  close(pb)
}

testit()
#> =======================================Error in testit() : oops

We could also try to ask R-core for a way to print a newline before the error message, that would be useful for txtProgressBar() as well, and many other places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants