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

Progress bar for non-interactive contexts #2221

Closed
3 tasks done
hadley opened this issue Feb 8, 2023 · 12 comments
Closed
3 tasks done

Progress bar for non-interactive contexts #2221

hadley opened this issue Feb 8, 2023 · 12 comments
Assignees
Labels
feature Feature requests

Comments

@hadley
Copy link
Contributor

hadley commented Feb 8, 2023

I think it would be useful for knitr to bundle a progress bar that's well suited to non-interactive contexts (i.e. when you're logging to a file and \r doesn't work). Here's one that I've been playing around this:

cat_line <- function(..., file = stdout()) {
  cat(paste0(..., "\n", collapse = ""), file = file)
}

batch_progress <- function(total, labels) {
  file <- getOption("knitr.progress.output", "")
  n_code <- sum(labels != "")

  update <- function(i) {
    lab <- labels[[i]]
    n_done <- sum(labels[seq_len(i)] != "")

    if (lab != "") {
      cat_line("Chunk: ", lab, " [", n_done, "/", n_code, "]", file = file)
    }
  }
  done <- function() {
    cat_line("Complete", file = file)
  }

  list(
    update = update,
    done = done
  )
}

By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • 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.
  • 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
Copy link
Contributor Author

hadley commented Feb 8, 2023

This sort of progress bar also works a little more nicely if you are logging data to stderr():

```{r alpha}
Sys.sleep(1)
cat("Reticulating splines\n", file = stderr())
```

```{r bravo}
Sys.sleep(1)
cat("Evaluating lambdas\n", file = stderr())
```

```{r charlie}
Sys.sleep(1)
cat("Integration factuals\n", file = stderr())
```

@hadley
Copy link
Contributor Author

hadley commented Feb 9, 2023

As an example of why the current default could be improved, here's the log file for an Rmd that I recently deployed to Posit Connect:

Screenshot 2023-02-09 at 10 50 49

@rich-iannone rich-iannone self-assigned this Feb 10, 2023
@rich-iannone rich-iannone added the feature Feature requests label Feb 10, 2023
@rich-iannone rich-iannone moved this to To discuss / To plan in R Markdown Team Projects Feb 10, 2023
@hadley
Copy link
Contributor Author

hadley commented Feb 10, 2023

One way to determine which progress bar to use would be to implement something like cli::is_dynamic_tty()

@yihui
Copy link
Owner

yihui commented Feb 13, 2023

This feature request is definitely reasonable. I wonder how hard it is to approximate cli::is_dynamic_tty() when cli is not installed. Would this be good enough?

isatty(getOption("knitr.progress.output", stdout())) || rstudioapi::isAvailable()

@hadley
Copy link
Contributor Author

hadley commented Feb 16, 2023

@yihui this would only be used if cli wasn't installed, right? IOTW is ok for knitr to suggest cli?

@gaborcsardi can you answer Yihui's question?

@gabor

This comment was marked as off-topic.

@hadley

This comment was marked as off-topic.

@gaborcsardi
Copy link

gaborcsardi commented Feb 17, 2023

Not perfect, but it is something. Are the build pane and the render pane relevant? They have \r support I believe, but I suspect you would get FALSE there?

You could also try to include other GUIs, see cli:::is_rapp_stdx(stream) and cli:::is_rkward_stdx(stream).

@yihui
Copy link
Owner

yihui commented Feb 17, 2023

is ok for knitr to suggest cli?

@hadley Yes, I'll definitely suggest cli.

Not perfect, but it is something. Are the build pane and the render pane relevant? They have \r support I believe, but I suspect you would get FALSE there?

@gaborcsardi This is just a fallback (when cli is not available) so doesn't need to be perfect. I just tested it and rstudioapi::isAvailable() did return FALSE but rstudioapi::versionInfo() returns information about RStudio, so I guess I can use the latter.

Thanks!

@gaborcsardi
Copy link

OTOH the job pane did not have \r support the last time I checked, so ideally it would return FALSE there. See the dynamic_tty entries here for the capabilities: https://github.com/r-lib/cli/blob/01f932adafd58a59d596b745bf88359c5cb5a969/R/aab-rstudio-detect.R#L205-L291

You could also copy this file, it should be self-contained. But yeah, for a fallback a good-enough solution is perfectly fine.

@rich-iannone rich-iannone assigned yihui and unassigned rich-iannone Feb 21, 2023
@yihui yihui closed this as completed in 47bce01 May 10, 2023
@github-project-automation github-project-automation bot moved this from To discuss / To plan to Done in R Markdown Team Projects May 10, 2023
@yihui
Copy link
Owner

yihui commented May 10, 2023

Now the bar will be suppressed when knitr.progress.output is not a connection (e.g., a file instead), or the connection is not isatty(). It's also possible to manually specify options(knitr.progress.simple = TRUE/FALSE) if the default is not good (TRUE means no bar).

Copy link

github-actions bot commented Nov 8, 2023

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

6 participants