tidyverse / reprex

Render bits of R code for sharing, e.g., on GitHub or StackOverflow.

Home Page:https://reprex.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot reprex `traceback()`

jooyoungseo opened this issue · comments

This is a side note while working on rstudio/tensorflow#494.

reprex::reprex() doesn't work as expected against traceback().

Reprex

  1. Copy and paste the following code in R console:
stop("test")
traceback()
  1. Run reprex::reprex().

Expected Results

stop("test")
#> Error: test                                                                                                                                            

traceback()
#> 1: stop("test")                                                                                                                                        

Current Results

stop("test")
#> Error in eval(expr, envir, enclos): test
traceback()
#> No traceback available

Created on 2022-01-04 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       Windows 10 x64 (build 19044)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.1252
#>  ctype    English_United States.1252
#>  tz       America/Chicago
#>  date     2022-01-04
#>  pandoc   2.16.2 @ C:/PROGRA~3/CHOCOL~1/bin/ (via rmarkdown)
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version    date (UTC) lib source
#>  cli           3.1.0      2021-10-27 [1] CRAN (R 4.1.2)
#>  crayon        1.4.2      2021-10-29 [1] CRAN (R 4.1.2)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.2)
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.2)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.2)
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.2)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.2)
#>  fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.2)
#>  glue          1.6.0      2021-12-17 [1] CRAN (R 4.1.2)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.2)
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.2)
#>  knitr         1.37       2021-12-16 [1] CRAN (R 4.1.2)
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.2)
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.2)
#>  pillar        1.6.4      2021-10-18 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.2)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.2)
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.1.2)
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.1.1)
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.1.1)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.1.2)
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.2)
#>  rlang         0.4.12     2021-10-18 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.11       2021-09-14 [1] CRAN (R 4.1.2)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.2)
#>  stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.2)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.2)
#>  styler        1.6.2.9000 2021-12-29 [1] Github (r-lib/styler@f672907)
#>  tibble        3.1.6      2021-11-07 [1] CRAN (R 4.1.2)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.2)
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.2)
#>  withr         2.4.3      2021-11-30 [1] CRAN (R 4.1.2)
#>  xfun          0.29       2021-12-14 [1] CRAN (R 4.1.2)
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.1)
#> 
#>  [1] C:/Program Files/R/R-4.1.2/library
#> 
#> ------------------------------------------------------------------------------

This is a limitation that comes from knitr / rmarkdown and therefore reprex just inherits it.

We can remove reprex from the equation and demonstrate this.

If you save the following as foo.Rmd:

---
output: github_document
---

`traceback()` doesn't work in R Markdown.

```{r error = TRUE}
stop("test")
traceback()
```

and then call rmarkdown::render("foo.Rmd"), the rendered result is:


traceback() doesn’t work in R Markdown.

stop("test")
## Error in eval(expr, envir, enclos): test
traceback()
## No traceback available

Now, it is also true that it is possible to promote base errors to rlang errors and thereby gain access to rlang's richer machinery for inspecting the back trace. But last time I checked, it was not clear exactly what reprex should do to simultaneously do this promotion and display errors in Rmd. In any case, the matter of optimizing how reprex uses these rlang features is being tracked in #377.