-
-
Notifications
You must be signed in to change notification settings - Fork 877
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 labels to the default progress bar and allow users to provide a custom progress bar #2196
Conversation
…tom progress bars close #1880
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
This seems like a nice improvement and it will make a lot of people happy. I think there are two small things that you might consider. Firstly, I was a little surprised that options(knitr.progress.fun = function(total) {
id = cli::cli_progress_bar(total = total, .auto_close = FALSE)
list(
update = function(i, label = NULL) {
cli::cli_progress_update(id = id)
},
done = function() {
cli::cli_process_done(id)
}
)
}) And I think it would be nice if when the label was empty you automatically generated the standard "unnamed chunk {i}" label, to avoid every progress bar implementer having to do that themselves. Secondly, you might also consider bundling two "default" progress bars with knitr, one that's used when batch_pb = function(total, labels) {
s = ifelse(labels == '', '', sprintf(' (%s)', labels))
list(
update = function(i) {
cat(s[i], "\n")
},
done = function() {
}
)
} I don't know how you tell if |
Excellent. For progressr, the following works: options(knitr.progress.fun = function(total, labels) {
## When creating a progressor it registers an on.exit() that
## shuts it downw automatically when exiting the function.
## To prevent this, we need need to use on_exit = FALSE.
p <- progressr::progressor(total, auto_finish = FALSE, on_exit = FALSE)
list(
update = function(i) {
p(message = sprintf('chunk: %s', labels[i]))
},
done = function() {
p(type = 'finish')
}
)
}) That'll signal progressr::handlers(global = TRUE) To tweak the progress reporter, to say, progress, use: progressr::handlers("progress") A reporter for cli is still on the to-do list (futureverse/progressr#123) I'll see if I can add something like options(knitr.progress.fun = progressr::make_knitr_progress_fcn()) with options to configure the |
Maybe I misunderstand the comment, but shouldn't all progress output to the terminal just be sent to stderr, e.g. |
@HenrikBengtsson I mean outside of R; i.e. you're not rendering interactively but are running |
Thanks @hadley and @HenrikBengtsson!
Passing to the constructor gives users a little more freedom, e.g., they can calculate the maximum width of all labels to pad enough spaces to shorter labels (this is what I did in the default progress function). The cli::cli_progress_update(id = id, status = labels[i]) but it didn't show the labels. Perhaps I need to use
They don't need to do that---
I'll see if it's possible to check whether |
@yihui ok, that makes sense. What's a text chunk? The non-code contents of an rmd? |
Yes (prose/narratives). |
…t)`, e.g., it can be `stderr()`
I'm not entirely sure what they meant by "ugly output". I tried the It doesn't seem to be possible to tell if the For now, I'll just leave this issue behind until someone reports it and requests changes. |
BTW, |
* master: make negative times 0 replace highr:::spaces() with strrep() in base R support chunk options message/warning = NA close #2204: drop the support for cairoDevice amend 1a0f2cc: make line_count() a few times faster Replace several stringr-based function calls with base equivalents (#2202, #1549) see if this fix the ruby gems issue https://github.com/yihui/knitr/actions/runs/3682745993/jobs/6230638404 stop importing xfun::isFALSE() and define isFALSE() only for R < 3.5.0 Add labels to the default progress bar and allow users to provide a custom progress bar (#2196)
Users can customize the progress bar via
options(knitr.progress.fun = function(total, labels) {})
. The function should create a progress bar and return two methods,update()
anddone()
.txt_pb()
is the default.knitr.progress.fun
function (total
is the total number of chunks;labels
is the vector of chunk labels---for text chunks, the labels are empty strings).An example of using the cli progress bar:
An example of progressr (not working---I'm not familiar with this package and couldn't figure out how to make it work):
To disable the progress bar:
Note that the global option
knitr.progress.fun
has to be set beforeknitr::knit()
starts. That usually means this option needs to be set in.Rprofile
.