yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.37k stars 874 forks source link

Minor progress bar infelicity #2220

Open hadley opened 1 year ago

hadley commented 1 year ago

I am loving the new progress bar! But I noticed a couple of small issues exemplified by this simple Rmd:

```{r alpha}
Sys.sleep(0.5)
Sys.sleep(0.5)
Sys.sleep(5)


After alpha and bravo have completed and charlie is still running, I see this:

<img width="804" alt="Screenshot 2023-02-08 at 13 00 06" src="https://user-images.githubusercontent.com/4196/217626563-64eee875-9160-4ca9-a7cf-f834702e3e80.png">

Two small problems:

* Weirdly, the `processing` message appears part way in the middle of the progress bar
* The progress bar shows 100% complete even though we're still working on chunk 3.

<!--
Please keep the below portion in your issue. Your issue will be closed if any of the boxes is not checked (i.e., replace `[ ]` by `[x]`). In certain (rare) cases, you may be exempted if you give a brief explanation (e.g., you are only making a suggestion for improvement). Thanks!
-->

---

By filing an issue to this repo, I promise that

- [x] I have fully read the issue guide at https://yihui.org/issue/.
- [x] I have provided the necessary information about my issue.
    - If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    - If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('knitr')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('yihui/knitr')`.
    - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.
hadley commented 1 year ago

Even more nitpicky, I noticed that if I run rmarkdown::render() there are two blank lines before the progress bar starts:

> rmarkdown::render("delay.Rmd")

processing file: delay.Rmd
  |........................                                                 |  33% (alpha)  
hadley commented 1 year ago

And maybe there's one extra empty line at the bottom too? (at least when run_pandoc = FALSE).

> invisible(rmarkdown::render("delay.Rmd", run_pandoc = FALSE))

processing file: delay.Rmd

output file: delay.knit.md

> 
hadley commented 1 year ago

Hmmm, the first problem (where "processing..." is interleaved in the output") might actually be an RStudio problem

hadley commented 1 year ago

It is an RStudio bug: https://github.com/rstudio/rstudio/issues/8160. knitr could work around it by outputing the "Process file" message to stdout() instead of stderr(). We have some techniques to do this in rlang/cli, but you could also just switch the message() to cat().

@kevinushey pointed me to this article on why interleaving streams is generally hard: https://unix.stackexchange.com/questions/476080/what-prevents-stdout-stderr-from-interleaving/476089#476089. You might also argue that maybe the problem is that the progress bar is using stdout() since everything that appears in this display will be using stderr()

hadley commented 1 year ago

Not 100% sure this is the same problem, but note that in this connnect log the progress bar shows up after the error.

Screenshot 2023-02-13 at 16 03 18
kevinushey commented 1 year ago

This is almost surely an RStudio issue -- not sure if there's a way to replicate the same flow in a "regular" R instance outside of RStudio.