r-lib / cli

Tools for making beautiful & useful command line interfaces
https://cli.r-lib.org/
Other
655 stars 70 forks source link

Suggestions for printing tibble preview in `cli_abort`? #699

Open dshemetov opened 5 months ago

dshemetov commented 5 months ago

Hello there. Thank you for your work on this package!

Do you have any suggestions for how to include tibble previews in error messages? Currently, our work around is something along the lines of: use the built-in tibble print with capture output, collapse the resulting character vector with newlines, and then insert that into a cli_abort message. Are there more ergonomic ways to do this or perhaps a different set of best practices?

We use cli in the internals of a time series forecasting package built on top of tidymodels, where it is very helpful to print the data context in an error because errors often stem from data anomalies.

cc @brookslogan

gaborcsardi commented 5 months ago

I don't think this is possible currently, unfortunately. cli_abort() prints a list of bullet points, where each bullet point is a paragraph that can only have inline formatting.

d-morrison commented 4 months ago

Seconding this question! With rlang::abort(), it seems like the body argument can be used:

rlang::abort(message = "missing strata", body = c("here:", dplyr::starwars |> capture.output()))
#> Error:
#> ! missing strata
#> here:
#> # A tibble: 87 × 14
#>    name     height  mass hair_color skin_color eye_color birth_year sex   gender
#>    <chr>     <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
#>  1 Luke Sk…    172    77 blond      fair       blue            19   male  mascu…
#>  2 C-3PO       167    75 <NA>       gold       yellow         112   none  mascu…
#>  3 R2-D2        96    32 <NA>       white, bl… red             33   none  mascu…
#>  4 Darth V…    202   136 none       white      yellow          41.9 male  mascu…
#>  5 Leia Or…    150    49 brown      light      brown           19   fema… femin…
#>  6 Owen La…    178   120 brown, gr… light      blue            52   male  mascu…
#>  7 Beru Wh…    165    75 brown      light      blue            47   fema… femin…
#>  8 R5-D4        97    32 <NA>       white, red red             NA   none  mascu…
#>  9 Biggs D…    183    84 black      light      brown           24   male  mascu…
#> 10 Obi-Wan…    182    77 auburn, w… fair       blue-gray       57   male  mascu…
#> # ℹ 77 more rows
#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> #   vehicles <list>, starships <list>

Created on 2024-07-17 with reprex v2.1.0

But cli_abort() loses the spacing:

cli::cli_abort(message = "missing strata", body = c("here:", dplyr::starwars |> capture.output()))
#> Error:
#> ! missing strata
#> here:
#> # A tibble: 87 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Luke Sk… 172 77 blond fair blue 19 male mascu…
#> 2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
#> 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
#> 4 Darth V… 202 136 none white yellow 41.9 male mascu…
#> 5 Leia Or… 150 49 brown light brown 19 fema… femin…
#> 6 Owen La… 178 120 brown, gr… light blue 52 male mascu…
#> 7 Beru Wh… 165 75 brown light blue 47 fema… femin…
#> 8 R5-D4 97 32 <NA> white, red red NA none mascu…
#> 9 Biggs D… 183 84 black light brown 24 male mascu…
#> 10 Obi-Wan… 182 77 auburn, w… fair blue-gray 57 male mascu…
#> # ℹ 77 more rows
#> # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>

Created on 2024-07-17 with reprex v2.1.0

I think rlang::abort() will be fine for my current purposes, but I'm wondering if there's a way to make cli::cli_abort() work as well?