r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
500 stars 137 forks source link

using waldo::compare inside of rlang::abort inserts a bullet point for only one of the comparisons #1585

Closed njtierney closed 1 year ago

njtierney commented 1 year ago

I initially mentioned this in the cli package but following the issues it seems it is a known issue (https://github.com/r-lib/cli/issues/586) - so I am using rlang::abort as suggested in one of the linked issues, but ran into a slightly annoying formatting issue:

check_age_breaks <- function(x,
                             y){
  if (!identical(x, y)){

    compare_res <- waldo::compare(
      x = x,
      y = y
    )

    rlang::abort(
      c(
        "age breaks are not the same:",
        compare_res
      )
    )
  }
}

vec1 <- c(1, 2, 3)
vec2 <- c(1, 2, 4)

waldo::compare(vec1, vec2)
#> `old`: 1 2 3
#> `new`: 1 2 4

check_age_breaks(vec1,vec2)
#> Error in `check_age_breaks()`:
#> ! age breaks are not the same:
#> • `old`: 1 2 3
#> `new`: 1 2 4

#> Backtrace:
#>     ▆
#>  1. └─global check_age_breaks(vec1, vec2)
#>  2.   └─rlang::abort(c("age breaks are not the same:", compare_res))

Created on 2023-03-06 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31) #> os macOS Ventura 13.2 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Australia/Hobart #> date 2023-03-06 #> pandoc 2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.0) #> diffobj 0.3.5 2021-10-05 [1] CRAN (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.0) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.0) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.0) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.0) #> rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.0) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> styler 1.9.0 2023-01-15 [1] CRAN (R 4.2.0) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.0) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.0) #> vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.2.0) #> waldo 0.4.0 2022-03-16 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.37 2023-01-31 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

This is slightly annoying as the nice waldo::compare isn't aligned nicely

#> • `old`: 1 2 3
#> `new`: 1 2 4

And ideally it would be either

#> • `old`: 1 2 3
#> • `new`: 1 2 4

or

#> `old`: 1 2 3
#> `new`: 1 2 4
njtierney commented 1 year ago

It seems that adding a named argument in the message help aviod this issue?

check_age_breaks1 <- function(x,
                             y){
  if (!identical(x, y)){

    compare_res <- waldo::compare(
      x = x,
      y = y
    )

    rlang::abort(
      c(
        "age breaks are not the same:",
        compare_res
      )
    )
  }
}

check_age_breaks2 <- function(x,
                             y){
  if (!identical(x, y)){

    compare_res <- waldo::compare(
      x = x,
      y = y
    )

    rlang::abort(
      c(
        "age breaks are not the same:",
        compare_res,
        i = "ensure they are the same"
      )
    )
  }
}

vec1 <- c(1,2,3,Inf)
vec2 <- c(1,2,3)

check_age_breaks1(vec1, vec2)
#> Error in `check_age_breaks1()`:
#> ! age breaks are not the same:
#> • `old`: 1 2 3 Inf
#> `new`: 1 2 3

#> Backtrace:
#>     ▆
#>  1. └─global check_age_breaks1(vec1, vec2)
#>  2.   └─rlang::abort(c("age breaks are not the same:", compare_res))
check_age_breaks2(vec1, vec2)
#> Error in `check_age_breaks2()`:
#> ! age breaks are not the same:
#> `old`: 1 2 3 Inf
#> `new`: 1 2 3    
#> ℹ ensure they are the same

#> Backtrace:
#>     ▆
#>  1. └─global check_age_breaks2(vec1, vec2)
#>  2.   └─rlang::abort(c("age breaks are not the same:", compare_res, i = "ensure they are the same"))

Created on 2023-03-06 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31) #> os macOS Ventura 13.2 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Australia/Hobart #> date 2023-03-06 #> pandoc 2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.0 2023-01-09 [1] CRAN (R 4.2.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.0) #> diffobj 0.3.5 2021-10-05 [1] CRAN (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.0) #> evaluate 0.20 2023-01-17 [1] CRAN (R 4.2.0) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.6.1 2023-02-06 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.0) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.0) #> rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.0) #> rmarkdown 2.20 2023-01-19 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> styler 1.9.0 2023-01-15 [1] CRAN (R 4.2.0) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.0) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.2.0) #> vctrs 0.5.2 2023-01-23 [1] CRAN (R 4.2.0) #> waldo 0.4.0 2022-03-16 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.37 2023-01-31 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
lionel- commented 1 year ago

The rlang-level bullets is only a fallback. The official implementation goes through cli (you can set it for abort() by calling on_load(local_use_cli()) in your package). So we're not really looking into improving or changing the implementation much.

But I think this rlang behaviour in the fallback case is expected. Historically an unnamed vector has been treated as a vector of "*" bullets by default.

You can set the names to override this:

abort(c("foo", "bar", "baz"))
#> Error:
#> ! foo
#> • bar
#> • baz

abort(set_names(c("foo", "bar", "baz"), ""))
#> Error:
#> ! foo
#> bar
#> baz

The interaction between waldo::compare() and cli::cli_abort() is a bit awkard because the former returns a single string with collapsed lines whereas the latter requires a vector of lines. It might make sense for waldo to offer a variant that returns a vector of lines.