Skip to content

Instantly share code, notes, and snippets.

@simonpcouch
Created October 24, 2022 17:00
Show Gist options
  • Save simonpcouch/842a972abce0b854e43bb5698f70cd3b to your computer and use it in GitHub Desktop.
Save simonpcouch/842a972abce0b854e43bb5698f70cd3b to your computer and use it in GitHub Desktop.
library(tidymodels)
library(cli)

The tune package has machinery to catch and log errors and warnings that occur while evaluating proposed models against resamples.

At the moment, we print those warnings/errors out one-by-one as they appear during evaluation.

res <- 
  fit_resamples(
    linear_reg(engine = "glmnet", penalty = 100),
    mpg ~ .,
    bootstraps(mtcars, 3)
  )
#> ! Bootstrap1: internal: A correlation computation is required, but `estimate` is constant and ha...
#> ! Bootstrap2: internal: A correlation computation is required, but `estimate` is constant and ha...
#> ! Bootstrap3: internal: A correlation computation is required, but `estimate` is constant and ha...

After evaluation, if the resulting object is printed out, we have some machinery to deduplicate repeated warnings and errors.

res
#> # Resampling results
#> # Bootstrap sampling 
#> # A tibble: 3 × 4
#>   splits          id         .metrics         .notes          
#>   <list>          <chr>      <list>           <list>          
#> 1 <split [32/12]> Bootstrap1 <tibble [2 × 4]> <tibble [1 × 3]>
#> 2 <split [32/10]> Bootstrap2 <tibble [2 × 4]> <tibble [1 × 3]>
#> 3 <split [32/15]> Bootstrap3 <tibble [2 × 4]> <tibble [1 × 3]>
#> 
#> There were issues with some computations:
#> 
#>   - Warning(s) x3: A correlation computation is required, but `estimate` is constant...
#> 
#> Run `show_notes(.Last.tune.result)` for more information.

Instead of printing out warnings/errors out one-by-one during evaluation, which can be cumbersome with many resamples (25 is default, in this case), I’d like to instead use the progress bar machinery to update summarizations of the warnings and errors.

e.g., something like:

#> Evaluating with resamples ■■■■■■■■■■■■■■■            66%
#> 
#> There were issues with some computations:
#> 
#>   - Warning(s) x2: A correlation computation is required, but `estimate` is constant...

Where that first line is the usual cli_progress_bar format string, and the text updates as evaluation occurs.

The issue that I run into is the the format argument seems to be collapsed into a single line and abbreviated.

As a more minimal example, this works:

test_bar <- function() {
  issues <- 0
  
  cli_progress_bar(
    type = "custom",
    total = 100,
    format = "Tuning! {cli::pb_bar} {issues} issues."
  )
  
  for (i in 1:100) {
    Sys.sleep(5/100)
    issues <- list("i" = i)
    cli_progress_update()
  }
  
  cli_progress_done()
}

test_bar()
#> Tuning! ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100 issues.

…though I’ve been unable to figure out how specify format in a way that respects newlines.

Created on 2022-10-24 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment