r-for-educators / flair

decorate your R code
https://r-for-educators.github.io/flair/index.html
Other
211 stars 21 forks source link

`decorate_code()` doesn't work with `stringr` >= 1.5.0 #42

Closed gadenbuie closed 1 year ago

gadenbuie commented 1 year ago
library(flair)

decorate_code("runif(1)", eval = FALSE) %>%
  flair("runif", background = "pink", eval = FALSE)
#> Error in `purrr::map()`:
#> ℹ In index: 1.
#> Caused by error in `str_extract_all()`:
#> ! `string` must be a vector, not a <source> object.

As far as I can tell, the error seems to be coming from this expression where at this point x is a list with class source and one item: list(src = "runif(1)").

Before, in stringr 1.4.1, str_extract_all() treated the source object as a list

library(stringr)
packageVersion("stringr")
#> [1] '1.4.1'

x <- structure(list(src = "runif(1)"), class = "source")
str_extract_all(x, "runif")
#> [[1]]
#> [1] "runif"

but in stringr 1.5.0 it now throws an error that the function doesn't know how to work with source objects.

library(stringr)
packageVersion("stringr")
#> [1] '1.5.0'

x <- structure(list(src = "runif(1)"), class = "source")
str_extract_all(x, "runif")
#> Error in `str_extract_all()`:
#> ! `string` must be a vector, not a <source> object.

#> Backtrace:
#>     ▆
#>  1. ├─stringr::str_extract_all(x, "runif")
#>  2. │ └─stringr:::check_lengths(string, pattern)
#>  3. │   └─vctrs::vec_size_common(...)
#>  4. └─vctrs:::stop_scalar_type(`<fn>`(`<source>`), "string", `<env>`)
#>  5.   └─vctrs:::stop_vctrs(...)
#>  6.     └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))

Interestingly, if you include the list class when creating the source object, str_extract_all() is happy:

library(stringr)
packageVersion("stringr")
#> [1] '1.5.0'

x <- structure(list(src = "runif(1)"), class = c("source", "list"))
str_extract_all(x, "runif")
#> [[1]]
#> [1] "runif"

At this point, I have to pause and thank reprex once again. I started looking into this issue having no idea how I would create an example and thinking the problem would require a deep dive into flair's internals. Almost as soon as I got to the minimal reprex, the answer was revealed to me. I'll submit a PR with a fix shortly 😄

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.3 (2020-02-29) #> os macOS 10.16 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2023-01-04 #> pandoc 2.19.2 @ /usr/local/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.4.1 2022-09-23 [1] CRAN (R 3.6.3) #> digest 0.6.30 2022-10-18 [1] CRAN (R 3.6.3) #> evaluate 0.18 2022-11-07 [1] CRAN (R 3.6.3) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 3.6.3) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 3.6.2) #> flair * 0.0.2 2022-03-07 [1] Github (r-for-educators/flair@1232fd5) #> fs 1.5.2 2021-12-08 [1] CRAN (R 3.6.2) #> glue 1.6.2 2022-02-24 [1] CRAN (R 3.6.3) #> highr 0.9 2021-04-16 [1] CRAN (R 3.6.2) #> htmltools 0.5.3 2022-07-18 [1] CRAN (R 3.6.3) #> knitr 1.40 2022-08-24 [1] CRAN (R 3.6.3) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 3.6.3) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 3.6.3) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 3.6.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.0) #> purrr 1.0.0 2022-12-20 [1] CRAN (R 3.6.3) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 3.6.3) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 3.6.3) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 3.6.3) #> R.utils 2.12.0 2022-06-28 [1] CRAN (R 3.6.3) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 3.6.2) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 3.6.3) #> rmarkdown 2.18 2022-11-09 [1] CRAN (R 3.6.3) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 3.6.2) #> stringi 1.7.8 2022-07-11 [1] CRAN (R 3.6.3) #> stringr 1.5.0 2022-12-02 [1] CRAN (R 3.6.3) #> styler 1.7.0 2022-03-13 [1] CRAN (R 3.6.3) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 3.6.3) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 3.6.2) #> vctrs 0.5.1 2022-11-16 [1] CRAN (R 3.6.3) #> withr 2.5.0 2022-03-03 [1] CRAN (R 3.6.3) #> xfun 0.32 2022-08-10 [1] CRAN (R 3.6.3) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 3.6.3) #> #> [1] /Users/garrick/Library/R/3.6/library #> [2] /Library/Frameworks/R.framework/Versions/3.6/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
gadenbuie commented 1 year ago

Oh and of course there's already a fix in place in #40.