tibble icon indicating copy to clipboard operation
tibble copied to clipboard

Error when using `tibble::as_tibble()` interactively not reproducing in `reprex::reprex()`

Open billdenney opened this issue 3 months ago • 7 comments

When I run the below code interactively, I get the following error, but when I run it in reprex, there is no error.

Error in • `ansi_strwrap(text, exdent = exdent, width = app$get_width(extra = padding))`:
! Internal error in `cli::ansi_strwrap()`
foo <- list(`Timepoint 
      (hr)` = "a", `Timepoint (day)` = "a", `1501` = "a", 
      `1502` = "a", `1503` = "a", "a", `30 mg/kg` = "a", NA, NA, 
      NA, NA, NA, NA, NA, NA, NA, NA, `Timepoint 
      (hr)` = "a", 
      `Timepoint (day)` = "a", `1501` = "a", `1502` = "a", `1503` = "a")
foo_nm <-
  setNames(foo, c("Timepoint \r\n(hr)", "Timepoint (day)", "1501", "1502", "1503", 
                "", "30 mg/kg", "", "", "", "", "", "", "", "", "", "", "Timepoint \r\n(hr)", 
                "Timepoint (day)", "1501", "1502", "1503"))
foo_tibble <- tibble::as_tibble(foo_nm, .name_repair = "unique")
#> New names:
#> • `Timepoint (hr)` -> `Timepoint (hr)...1`
#> • `Timepoint (day)` -> `Timepoint (day)...2`
#> • `1501` -> `1501...3`
#> • `1502` -> `1502...4`
#> • `1503` -> `1503...5`
#> • `` -> `...6`
#> • `` -> `...8`
#> • `` -> `...9`
#> • `` -> `...10`
#> • `` -> `...11`
#> • `` -> `...12`
#> • `` -> `...13`
#> • `` -> `...14`
#> • `` -> `...15`
#> • `` -> `...16`
#> • `` -> `...17`
#> • `Timepoint (hr)` -> `Timepoint (hr)...18`
#> • `Timepoint (day)` -> `Timepoint (day)...19`
#> • `1501` -> `1501...20`
#> • `1502` -> `1502...21`
#> • `1503` -> `1503...22`

Created on 2025-09-22 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :
#> running command '"quarto"
#> TMPDIR=C:/Users/wdenn/AppData/Local/Temp/RtmpGczJSL/file7a88368f6b27 -V' had
#> status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.0 (2025-04-11 ucrt)
#>  os       Windows 11 x64 (build 26100)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/New_York
#>  date     2025-09-22
#>  pandoc   3.6.3 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>  quarto   NA @ C:\\PROGRA~1\\Quarto\\bin\\quarto.exe
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.3   2025-01-10 [1] CRAN (R 4.5.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.5.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.5.0)
#>  pillar        1.10.2  2025-04-05 [1] CRAN (R 4.5.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.5.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.5.0)
#>  rstudioapi    0.17.1  2024-10-22 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] CRAN (R 4.5.0)
#>  tibble        3.3.0   2025-06-08 [1] CRAN (R 4.5.1)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.5.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.52    2025-04-02 [1] CRAN (R 4.5.0)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] C:/Users/wdenn/AppData/Local/R/win-library/4.5
#>  [2] C:/Program Files/R/R-4.5.0/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

billdenney avatar Sep 22 '25 22:09 billdenney

Thanks. When you run interactively, what do you see with getOption("width") ? Can you replicate with a reprex by setting the corresponding width?

krlmlr avatar Sep 23 '25 04:09 krlmlr

Thanks for the quick response. My width was 179 characters, and it didn't replicate (see the same result below). I also updated the width to 71 and 182 characters.

foo <- list(`Timepoint 
      (hr)` = "a", `Timepoint (day)` = "a", `1501` = "a", 
      `1502` = "a", `1503` = "a", "a", `30 mg/kg` = "a", NA, NA, 
      NA, NA, NA, NA, NA, NA, NA, NA, `Timepoint 
      (hr)` = "a", 
      `Timepoint (day)` = "a", `1501` = "a", `1502` = "a", `1503` = "a")
foo_nm <-
  setNames(foo, c("Timepoint \r\n(hr)", "Timepoint (day)", "1501", "1502", "1503", 
                "", "30 mg/kg", "", "", "", "", "", "", "", "", "", "", "Timepoint \r\n(hr)", 
                "Timepoint (day)", "1501", "1502", "1503"))
options(width = 179)
foo_tibble <- tibble::as_tibble(foo_nm, .name_repair = "unique")
#> New names:
#> • `Timepoint (hr)` -> `Timepoint (hr)...1`
#> • `Timepoint (day)` -> `Timepoint (day)...2`
#> • `1501` -> `1501...3`
#> • `1502` -> `1502...4`
#> • `1503` -> `1503...5`
#> • `` -> `...6`
#> • `` -> `...8`
#> • `` -> `...9`
#> • `` -> `...10`
#> • `` -> `...11`
#> • `` -> `...12`
#> • `` -> `...13`
#> • `` -> `...14`
#> • `` -> `...15`
#> • `` -> `...16`
#> • `` -> `...17`
#> • `Timepoint (hr)` -> `Timepoint (hr)...18`
#> • `Timepoint (day)` -> `Timepoint (day)...19`
#> • `1501` -> `1501...20`
#> • `1502` -> `1502...21`
#> • `1503` -> `1503...22`

Created on 2025-09-23 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", : running command '"quarto" TMPDIR=C:/Users/wdenn/AppData/Local/Temp/RtmpWmoumq/filed6b048c15541 -V' had
#> status 1
#> ─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.0 (2025-04-11 ucrt)
#>  os       Windows 11 x64 (build 26100)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/New_York
#>  date     2025-09-23
#>  pandoc   3.6.3 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>  quarto   NA @ C:\\PROGRA~1\\Quarto\\bin\\quarto.exe
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.3   2025-01-10 [1] CRAN (R 4.5.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.5.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.5.0)
#>  pillar        1.10.2  2025-04-05 [1] CRAN (R 4.5.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.5.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.5.0)
#>  rstudioapi    0.17.1  2024-10-22 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] CRAN (R 4.5.0)
#>  tibble        3.3.0   2025-06-08 [1] CRAN (R 4.5.1)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.5.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.52    2025-04-02 [1] CRAN (R 4.5.0)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] C:/Users/wdenn/AppData/Local/R/win-library/4.5
#>  [2] C:/Program Files/R/R-4.5.0/library
#> 
#> ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

billdenney avatar Sep 23 '25 12:09 billdenney

Thanks. I see the error in Positron too.

krlmlr avatar Sep 23 '25 13:09 krlmlr

Whew! I'm always worried about hard to reproduce issues like this. Please let me know if I can help troubleshoot more.

billdenney avatar Sep 23 '25 15:09 billdenney

I've just started running into this issue too. My job involves reading a lot of xlsx files supplied by clients, and the files are frequently formatted strangely. Our standard procedure has been to use read_excel(.name_repair = "universal"), which itself uses as_tibble(.name_repair = "universal"). We recently went through some system updates that allowed us to finally update our packages to current, and now we're seeing this error. We were able to trace this issue back to the console print when updating header names that contain space and a line break character.

Image

Here the original header name has a space after ID, followed by \r\n. Note that .name_repair = "universal_quiet" passes without an error. Here's another snip that highlights a bit more:

Image

Note the console print after the call to read_excel(.name_repair = "universal"), in particular the column called "XX\r\nYY" is printed as changing from "YY --> 'XX..YY'"; the part of the name before the line breaks do not print in the console. (read_excel_silent is our internal function that is just a suppressMessages() wrapper on read_excel(.name_repair = "universal"), since the 'universal_quiet' option didn't use to exist, and we didn't want the console print)

Here's a copy of Test.xlsx:

TEST.xlsx

and a copy of Book1.xlsx:

Book1.xlsx

the-travvo avatar Oct 03 '25 02:10 the-travvo

This seems to be a long-standing issue in the cli package. The only reliable workaround I can offer is to turn off verbose name repair.

https://github.com/r-lib/cli/issues/667

krlmlr avatar Oct 03 '25 16:10 krlmlr

Ha! I was also just taking a look at this! I'll add what I captured in order to be concrete about the work around, which is to use "unique_quiet" name repair instead of "unique". The problem is in the attempt to message about name repair, not doing the name repair itself.

Image

jennybc avatar Oct 03 '25 17:10 jennybc