GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cli/vignettes/progress.Rmd at cran-3.6.6 · r-lib/cli Expand file tree/ Copy pathprogress.RmdMore file actions 369 lines (302 loc) · 10.7 KB / Copy pathprogress.RmdFile metadata and controls369 lines (302 loc) · 10.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 --- title: "Introduction to progress bars in cli" author: "Gábor Csárdi" date: "`r Sys.Date()`" description: > Add progress bars to your loops and mapping functions. output: rmarkdown::html_document: toc: true toc_depth: 2 editor_options: markdown: wrap: sentence --- ```{r} #| label: asciicast-setup #| include: false #| cache: false knitr::opts_chunk$set( collapse = TRUE, comment = "#>", out.width = "100%", cache = TRUE, asciicast_at = "all", asciicast_cpp11_linkingto = "[[cpp11::linking_to(\"cli\")]]" ) asciicast::init_knitr_engine( echo = TRUE, echo_input = FALSE ) ``` # Introduction ```{asciicast} #| label: asciicast-tick-time #| include: false #| cache: false set.seed(1) Sys.setenv(CLI_TICK_TIME = "100") ``` ```{asciicast} #| label: progress-setup setup #| cache: false #| include: false library(cli) options(cli.progress_show_after = 0) options(cli.progress_clear = FALSE) options(cli.progress_format_iterator = NULL) options(cli.progress_format_iterator_nototal = NULL) options(cli.progress_format_tasks = NULL) options(cli.progress_format_tasks_nototal = NULL) options(cli.progress_format_download = NULL) options(cli.progress_format_download_nototal = NULL) ``` ```r library(cli) options(cli.progress_show_after = 0) options(cli.progress_clear = FALSE) ``` This document discusses the structure and simplest uses of the cli progress bar API. For more advanced usage and the C progress bar API, see the 'Advanced cli progress bars' article and the manual pages. From version 3.0.0 cli provides a set of functions to create progress bars. The main goals of the progress bar API are: * Reduce clutter. Try to avoid verbose syntax, unless necessary. * Flexibility from R and C/C++ code. Support all cli features in progress bars: glue interpolation, theming, pluralization, etc. * Predictably small performance penalty. A very small constant penalty per iteration, and a reasonable penalty per second. # The traditional progress bar API Add a progress bar in three steps: 1. Call `cli_progress_bar()` to add create a progress bar. 2. Call `cli_progress_update()` to update it. 3. Call `cli_progress_done()` to terminate it. For example: ```{asciicast} #| label: classic-example #| fig-alt: "Progress bar, that contains, from left to right, the specified label, the bar with green squares, the progress percentage, and the ETA." 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() ``` The traditional API provides full control w.r.t when to create, update and terminate a progress bar. # The current progress bar For conciseness, the progress bar functions refer to the _current progress bar_ by default. Every function has at most one current progress bar at any time. The current progress bar of a function is terminated when the function creates another progress bar or when the function returns, errors or is interrupted. The current progress bar lets us omit the `cli_progress_done()` call: ```{asciicast} #| label: current #| fig-alt: "Two progress bars, after the first finishes, the second starts and then finishes as well." clean <- function() { cli_progress_bar("Cleaning data #1", total = 100) for (i in 1:100) { Sys.sleep(3/100) cli_progress_update() } cli_progress_bar("Cleaning data #2", total = 100) for (i in 1:100) { Sys.sleep(3/100) cli_progress_update() } } clean() ``` # Unknown total number of units In some cases the total number of progress units is unknown, so simply omit them from `cli_progress_bar()` (or set them to `NA`). cli uses a different display when `total` is unknown: ```{asciicast} #| label: unknown-total-seed #| include: false #| cache: false set.seed(1) ``` ```{asciicast} #| label: unknown-total #| fig-alt: "Example progress bar where the total number of units is unknown. It has a spinner, the specified label, shows how many units are done, how many units are completed per second and the elapsed time." walk_dirs <- function() { cli_progress_bar("Walking directories") while (TRUE) { if (runif(1) < 0.01) break Sys.sleep(0.01) cli_progress_update() } cli_progress_update(force = TRUE) } walk_dirs() ``` # Quick loops By default, cli does not show progress bars that are terminated within two seconds after their creation. The end user can configure this limit with the `cli.progress_show_after` global option. For example, in this document we set the limit to zero seconds, so progress bars are shown at their first update. # Progress bars for mapping functions: `cli_progress_along()` `cli_progress_along()` is currently experimental. To add a progress bar to a call to `lapply()` or another mapping function, wrap the input sequence into `cli_progress_along()`: ```r lapply(cli_progress_along(X), fun) ``` `cli_progress_along()` works similarly to `seq_along()`, it returns an index vector. If you use `cli_progress_along()`, then `lapply()` will pass the _indices_ of the elements in `X` to `fun`, instead of the elements themselves. `cli_progress_along()` expects that the index vector will be used only once, from beginning to end. It is best to never assign the return value of `cli_progress_along()` to a variable. An example: ```{asciicast} #| label: tickalong #| fig-alt: "Progress bar with green squares, that also shows the progress percentage and the ETA." f <- function() { rawabc <- lapply( cli_progress_along(letters), function(i) { charToRaw(letters[i]) Sys.sleep(0.5) } ) } f() ``` `cli_progress_along()` uses ALTREP, so it only works from R 3.5.0 and later. On older R versions it is equivalent to `seq_along()` and it does not create a progress bar. ## `for` loops You can also use `cli_progress_along()` in `for` loops, with the additional complication that if you use `break`, then you might need to terminate the progress bar explicitly: ```r for (i in cli_progress_along(seq)) { ... if (cond) cli_progress_done() && break ... } ``` `cli_progress_done()` always returns `TRUE` to allow this form. Alternatively, you can terminate the progress bar right after loop: ```r for (i in cli_progress_along(seq)) { ... if (cond) break ... } cli_progress_done() ``` If the function containing the `for` loop returns after the loop, or you create another progress bar with `cli_progress_along()` or `cli_progress_bar()`, then no explicit `cli_progress_done()` is needed. # Simplified API Often you don't need the full power of the progress bar API, and only want to show a status message. The `cli_progress_message()` and `cli_progress_step()` functions are tailored for this. `cli_progress_message()` shows a (potentially templated) message in the status bar. For convenience, the progress bar rules still apply here by default: * Status messages are removed when their calling function exits. * A status message removes the previous status message or progress bar of the same caller function. ```{asciicast} #| label: cli_progress_message #| fig-alt: "The three messages are shown, each on its own line, the third one is iterated over 5 steps." f <- function() { cli_progress_message("Task one is running...") Sys.sleep(2) cli_progress_message("Task two is running...") Sys.sleep(2) step <- 1L cli_progress_message("Task three is underway: step {step}") for (step in 1:5) { Sys.sleep(0.5) cli_progress_update() } } f() ``` Status messages may use glue interpolation, cli styling and pluralization, as usual. You can call `cli_progress_update()` to update a status message. `cli_progress_step()` is slightly different from `cli_progress_message()`: * it adds cli's alert themes to the status messages (info, success or danger), * prints the duration of each step (by default), and * it keeps the messages on the screen after they are terminated. ```{asciicast} #| label: cli_progress_step_simple #| fig-alt: "Four progress steps are shown, each on its own line. Each steps show up as an 'i' (info) step first, and stays like that while it is running. When it is done, the 'i' is turned into a tick mark, and the running time of the step is added to the line at the end, in grey." f <- function() { cli_progress_step("Downloading data") Sys.sleep(2) cli_progress_step("Importing data") Sys.sleep(1) cli_progress_step("Cleaning data") Sys.sleep(2) cli_progress_step("Fitting model") Sys.sleep(3) } f() ``` As usual, you can use `cli_progress_step()` to update an existing status message. ```{asciicast} #| label: cli_progress_step #| fig-alt: "First the 'About to start..'. message is shown in its own line. Then a progress bar starts on the next line. The progress bar takes 10 steps. After the fifth step, the progress bar is overwritten with the 'Already half way!' message and the progress bar is moved down to the third line." f <- function(n = 10) { cli_alert_info("About to start downloads of {n} file{?s}") i <- 0 cli_progress_step("Got {i}/{n} {qty(i)}file{?s}.") for (i in seq_len(n)) { Sys.sleep(0.5) if (i == 5) cli_alert_info("Already half way!") cli_progress_update() } } f() ``` If you can update the status message frequently enough, then you can also add a spinner to it: ```{asciicast} #| label: cli_progress_step_spinner #| fig-alt: "Four steps are shown, each on its own line. While each step is running, its line has a spinner. Once it is done, the spinner turns into a tick mark and the running time of the step is added to its line at the end." f <- function() { cli_progress_step("Downloading data", spinner = TRUE) for (i in 1:100) { cli_progress_update(); Sys.sleep(2/100) } cli_progress_step("Importing data", spinner = TRUE) for (i in 1:100) { cli_progress_update(); Sys.sleep(1/100) } cli_progress_step("Cleaning data", spinner = TRUE) for (i in 1:100) { cli_progress_update(); Sys.sleep(2/100) } cli_progress_step("Fitting model", spinner = TRUE) for (i in 1:100) { cli_progress_update(); Sys.sleep(3/100) } } f() ``` `cli_progress_step()` automatically handles errors, and styles the status message accordingly: ```{asciicast} #| label: step-error #| fig-alt: "Two steps are shown, each in its own line. While a step is running its line is an 'i' (info) line. The first step finishes successfully and its 'i' mark is turned into a tick mark. The second step fails with an error and its line is overwritten with the error message, and moved down to the third line, and marked with an 'x' (error) mark." #| asciicast_rows: 3 f <- function() { cli_progress_step("First step, this will succeed") Sys.sleep(1) cli_progress_step("Second step, this will fail") Sys.sleep(1) stop("Something is wrong here") } f() ``` <!-- # Downloads --> <!-- ## Downloads with httr --> <!-- TODO --> <!-- ## Downloads with curl --> <!-- TODO --> |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |