EVOLUTION-MANAGER
Edit File: cli_progress_bar.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: cli progress bars</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for cli_progress_bar {cli}"><tr><td>cli_progress_bar {cli}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>cli progress bars</h2> <h3>Description</h3> <p>This is the reference manual of the three functions that create, update and terminate progress bars. For a tutorial see the <a href="https://cli.r-lib.org/articles/progress.html">cli progress bars</a>. </p> <p><code>cli_progress_bar()</code> creates a new progress bar. </p> <p><code>cli_progress_update()</code> updates the state of a progress bar, and potentially the display as well. </p> <p><code>cli_progress_done()</code> terminates a progress bar. </p> <h3>Usage</h3> <pre> cli_progress_bar( name = NULL, status = NULL, type = c("iterator", "tasks", "download", "custom"), total = NA, format = NULL, format_done = NULL, format_failed = NULL, clear = getOption("cli.progress_clear", TRUE), current = TRUE, auto_terminate = type != "download", extra = NULL, .auto_close = TRUE, .envir = parent.frame() ) cli_progress_update( inc = NULL, set = NULL, total = NULL, status = NULL, extra = NULL, id = NULL, force = FALSE, .envir = parent.frame() ) cli_progress_done(id = NULL, .envir = parent.frame(), result = "done") </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>name</code></td> <td> <p>This is typically used as a label, and should be short, at most 20 characters.</p> </td></tr> <tr valign="top"><td><code>status</code></td> <td> <p>New status string of the progress bar, if not <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>type</code></td> <td> <p>Type of the progress bar. It is used to select a default display if <code>format</code> is not specified. Currently supported types: </p> <ul> <li> <p><code>iterator</code>: e.g. a for loop or a mapping function, </p> </li> <li> <p><code>tasks</code>: a (typically small) number of tasks, </p> </li> <li> <p><code>download</code>: download of one file, </p> </li> <li> <p><code>custom</code>: custom type, <code>format</code> must not be <code>NULL</code> for this type. </p> </li></ul> </td></tr> <tr valign="top"><td><code>total</code></td> <td> <p>Total number of progress units, or <code>NA</code> if it is unknown. <code>cli_progress_update()</code> can update the total number of units. This is handy if you don't know the size of a download at the beginning, and also in some other cases. If <code>format</code> is set to <code>NULL</code>, <code>format</code> (plus <code>format_done</code> and <code>format_failed</code>) will be updated when you change <code>total</code> from <code>NA</code> to a number. I.e. default format strings will be updated, custom ones won't be.</p> </td></tr> <tr valign="top"><td><code>format</code></td> <td> <p>Format string. It has to be specified for custom progress bars, otherwise it is optional, and a default display is selected based on the progress bat type and whether the number of total units is known. Format strings may contain glue substitution, the support pluralization and cli styling. See <a href="progress-variables.html">progress-variables</a> for special variables that you can use in the custom format.</p> </td></tr> <tr valign="top"><td><code>format_done</code></td> <td> <p>Format string for successful termination. By default the same as <code>format</code>.</p> </td></tr> <tr valign="top"><td><code>format_failed</code></td> <td> <p>Format string for unsuccessful termination. By default the same as <code>format</code>.</p> </td></tr> <tr valign="top"><td><code>clear</code></td> <td> <p>Whether to remove the progress bar from the screen after it has terminated. Defaults to the <code>cli.progress_clear</code> option, or <code>TRUE</code> if unset.</p> </td></tr> <tr valign="top"><td><code>current</code></td> <td> <p>Whether to use this progress bar as the current progress bar of the calling function. See more at 'The current progress bar' below.</p> </td></tr> <tr valign="top"><td><code>auto_terminate</code></td> <td> <p>Whether to terminate the progress bar if the number of current units reaches the number of total units.</p> </td></tr> <tr valign="top"><td><code>extra</code></td> <td> <p>Extra data to add to the progress bar. This can be used in custom format strings for example. It should be a named list. <code>cli_progress_update()</code> can update the extra data. Often you can get away with referring to local variables in the format string, and then you don't need to use this argument. Explicitly including these constants or variables in <code>extra</code> can result in cleaner code. In the rare cases when you need to refer to the same progress bar from multiple functions, and you can them to <code>extra</code>.</p> </td></tr> <tr valign="top"><td><code>.auto_close</code></td> <td> <p>Whether to terminate the progress bar when the calling function (or the one with execution environment in <code>.envir</code> exits. (Auto termination does not work for progress bars created from the global environment, e.g. from a script.)</p> </td></tr> <tr valign="top"><td><code>.envir</code></td> <td> <p>The environment to use for auto-termination and for glue substitution. It is also used to find and set the current progress bar.</p> </td></tr> <tr valign="top"><td><code>inc</code></td> <td> <p>Increment in progress units. This is ignored if <code>set</code> is not <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>set</code></td> <td> <p>Set the current number of progress units to this value. Ignored if <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>id</code></td> <td> <p>Progress bar to update or terminate. If <code>NULL</code>, then the current progress bar of the calling function (or <code>.envir</code> if specified) is updated or terminated.</p> </td></tr> <tr valign="top"><td><code>force</code></td> <td> <p>Whether to force a display update, even if no update is due.</p> </td></tr> <tr valign="top"><td><code>result</code></td> <td> <p>String to select successful or unsuccessful termination. It is only used if the progress bar is not cleared from the screen. It can be one of <code>"done"</code>, <code>"failed"</code>, <code>"clear"</code>, and <code>"auto"</code>.</p> </td></tr> </table> <h3>Details</h3> <h4>Basic usage</h4> <p><code>cli_progress_bar()</code> creates a progress bar, <code>cli_progress_update()</code> updates an existing progress bar, and <code>cli_progress_done()</code> terminates it. </p> <p>It is good practice to always set the <code>name</code> argument, to make the progress bar more informative. </p> <div class="sourceCode r"><pre>clean <- function() { cli_progress_bar("Cleaning data", total = 100) for (i in 1:100) { Sys.sleep(5/100) cli_progress_update() } cli_progress_done() } clean() </pre></div> <p><img src="../help/figures/progress-1.svg" alt="progress-1.svg" /> </p> <h4>Progress bar types</h4> <p>There are three builtin types of progress bars, and a custom type. </p> <div class="sourceCode r"><pre>tasks <- function() { cli_progress_bar("Tasks", total = 3, type = "tasks") for (i in 1:3) { Sys.sleep(1) cli_progress_update() } cli_progress_done() } tasks() </pre></div> <p><img src="../help/figures/progress-tasks.svg" alt="progress-tasks.svg" /> </p> <h4>Unknown <code>total</code></h4> <p>If <code>total</code> is not known, then cli shows a different progress bar. Note that you can also set <code>total</code> in <code>cli_progress_update()</code>, if it not known when the progress bar is created, but you learn it later. </p> <div class="sourceCode r"><pre>nototal <- function() { cli_progress_bar("Parameter tuning") for (i in 1:100) { Sys.sleep(3/100) cli_progress_update() } cli_progress_done() } nototal() </pre></div> <p><img src="../help/figures/progress-natotal.svg" alt="progress-natotal.svg" /> </p> <h4>Clearing the progress bar</h4> <p>By default cli removes terminated progress bars from the screen, if the terminal supports this. If you want to change this, use the <code>clear</code> argument of <code>cli_progress_bar()</code>, or the <code>cli.progress_clear</code> global option (see <a href="cli-config.html">cli-config</a>) to change this. </p> <p>(In the cli documentation we usually set <code>cli.progress_clear</code> to <code>FALSE</code>, so users can see how finished progress bars look.) </p> <p>In this example the first progress bar is cleared, the second is not. </p> <div class="sourceCode r"><pre>fun <- function() { cli_progress_bar("Data cleaning", total = 100, clear = TRUE) for (i in 1:100) { Sys.sleep(3/100) cli_progress_update() } cli_progress_bar("Parameter tuning", total = 100, clear = FALSE) for (i in 1:100) { Sys.sleep(3/100) cli_progress_update() } } fun() </pre></div> <p><img src="../help/figures/progress-clear.svg" alt="progress-clear.svg" /> </p> <h4>Initial delay</h4> <p>Updating a progress bar on the screen is costly, so cli tries to avoid it for quick loops. By default a progress bar is only shown after two seconds. You can change this default with the <code>cli.progress_show_after</code> global option (see <a href="cli-config.html">cli-config</a>). </p> <p>(In the cli documentation we usually set <code>cli.progress_show_after</code> to <code>0</code> (zero seconds), so progress bars are shown immediately.) </p> <p>In this example we only show the progress bar after two seconds. </p> <div class="sourceCode r"><pre>fun <- function() { cli_alert("Starting now, at {Sys.time()}") cli_progress_bar( total = 100, format = "{cli::pb_bar} {pb_percent} @ {Sys.time()}" ) for (i in 1:100) { Sys.sleep(4/100) cli_progress_update() } } options(cli.progress_show_after = 2) fun() </pre></div> <p><img src="../help/figures/progress-after.svg" alt="progress-after.svg" /> </p> <h4>The <em>current</em> progress bar</h4> <p>By default cli sets the new progress bar as the <em>current</em> progress bar of the calling function. The current progress bar is the default one in cli progress bar operations. E.g. if no progress bar id is supplied in <code>cli_progress_update()</code>, then the current progress bar is updated. </p> <p>Every function can only have a single <em>current</em> progress bar, and if a new one is created, then the previous one (if any) is automatically terminated. The current progress bar is also terminated when the function that created it exits. Thanks to these rules, most often you don't need to explicitly deal with progress bar ids, and you don't need to explicitly call <code>cli_progress_done()</code>: </p> <div class="sourceCode r"><pre>fun <- function() { cli_progress_bar("First step ", total = 100) for (i in 1:100) { Sys.sleep(2/100) cli_progress_update() } cli_progress_bar("Second step", total = 100) for (i in 1:100) { Sys.sleep(2/100) cli_progress_update() } } fun() </pre></div> <p><img src="../help/figures/progress-current.svg" alt="progress-current.svg" /> </p> <h4>cli output while the progress bar is active</h4> <p>cli allows emitting regular cli output (alerts, headers, lists, etc.) while a progress bar is active. On terminals that support this, cli will remove the progress bar temporarily, emit the output, and then restores the progress bar. </p> <div class="sourceCode r"><pre>fun <- function() { cli_alert_info("Before the progress bar") cli_progress_bar("Calculating", total = 100) for (i in 1:50) { Sys.sleep(4/100) cli_progress_update() } cli_alert_info("Already half way!") for (i in 1:50) { Sys.sleep(4/100) cli_progress_update() } cli_alert_info("All done") } fun() </pre></div> <p><img src="../help/figures/progress-output.svg" alt="progress-output.svg" /> </p> <p>See also <code><a href="cli_progress_output.html">cli_progress_output()</a></code>, which sends text for the current progress handler. E.g. in a Shiny app it will send the output to the Shiny progress bar, as opposed to the <code>cli_alert()</code> etc. cli functions which will print the text to the console. </p> <h4>Custom formats</h4> <p>In addition to the builtin types, you can also specify a custom format string. In this case <a href="progress-variables.html">progress variables</a> are probably useful to avoid calculating some progress bar quantities like the elapsed time, of the ETA manually. You can also use your own variables in the calling function: </p> <div class="sourceCode r"><pre>fun <- function(urls) { cli_progress_bar( format = paste0( "{pb_spin} Downloading {.path {basename(url)}} ", "[{pb_current}/{pb_total}] ETA:{pb_eta}" ), format_done = paste0( "{col_green(symbol$tick)} Downloaded {pb_total} files ", "in {pb_elapsed}." ),, total = length(urls) ) for (url in urls) { cli_progress_update() Sys.sleep(5/10) } } fun(paste0("https://acme.com/data-", 1:10, ".zip")) </pre></div> <p><img src="../help/figures/progress-format.svg" alt="progress-format.svg" /> </p> <h3>Value</h3> <p><code>cli_progress_bar()</code> returns the id of the new progress bar. The id is a string constant. </p> <p><code>cli_progress_update()</code> returns the id of the progress bar, invisibly. </p> <p><code>cli_progress_done()</code> returns <code>TRUE</code>, invisibly, always. </p> <h3>See Also</h3> <p><code><a href="cli_progress_message.html">cli_progress_message()</a></code> and <code><a href="cli_progress_step.html">cli_progress_step()</a></code> for simpler progress messages. </p> <hr /><div style="text-align: center;">[Package <em>cli</em> version 3.4.1 <a href="00Index.html">Index</a>]</div> </body></html>