yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Python exception types not displayed #2316

Closed brry closed 5 months ago

brry commented 5 months ago

In Rnw Python chunks, exception types are no longer included in the output. Last year, the example below showed ## ValueError: invalid literal for int() with base 10: 'charstring' now, it only shows ## invalid literal for int() with base 10: 'charstring'

---
title: "ShowPyException"
output: beamer_presentation
---

## slide title

```{python, error=TRUE}
obj = "charstring"
int(obj)


---

By filing an issue to this repo, I promise that

- [x] I have fully read the issue guide at https://yihui.org/issue/.
- [x] I have provided the necessary information about my issue.
    - If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    - If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('knitr')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('yihui/knitr')`.
    - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045), RStudio 2023.12.0.369

Locale:
  LC_COLLATE=English_United States.1252 
  LC_CTYPE=German_Germany.utf8          
  LC_MONETARY=English_United States.1252
  LC_NUMERIC=C                          
  LC_TIME=English_United States.1252    
system code page: 65001

Package version:
  base64enc_0.1.3   bslib_0.6.0      
  cachem_1.0.8      cli_3.6.1        
  digest_0.6.33     ellipsis_0.3.2   
  evaluate_0.23     fastmap_1.1.1    
  fontawesome_0.5.2 fs_1.6.3         
  glue_1.6.2        graphics_4.3.1   
  grDevices_4.3.1   highr_0.10       
  htmltools_0.5.7   jquerylib_0.1.4  
  jsonlite_1.8.7    knitr_1.45.10    
  lifecycle_1.0.4   magrittr_2.0.3   
  memoise_2.0.1     methods_4.3.1    
  mime_0.12         R6_2.5.1         
  rappdirs_0.3.3    rlang_1.1.2      
  rmarkdown_2.25    sass_0.4.7       
  stats_4.3.1       stringi_1.8.2    
  stringr_1.5.1     tinytex_0.49     
  tools_4.3.1       utils_4.3.1      
  vctrs_0.6.4       xfun_0.41        
  yaml_2.3.7       

Pandoc version: 3.1.1
cderv commented 5 months ago

error = TRUE means to not let any error in the chunk to stop the knitting. (see doc at https://yihui.org/knitr/options/#text-output)

This is what I get in output image

where I can see the error.

If set to FALSE then the error is thrown during knitting.

==> rmarkdown::render('C:/Users/chris/Documents/test.Rmd',  encoding = 'UTF-8');

  |....................................................| 100% [unnamed-chunk-1]

processing file: test.Rmd

Quitting from lines 9-11 [unnamed-chunk-1] (test.Rmd)
Error in `py_call_impl()`:
! ValueError: invalid literal for int() with base 10: 'charstring'
Run `reticulate::py_last_error()` for details.
Backtrace:
  1. rmarkdown::render("C:/Users/chris/Documents/test.Rmd", encoding = "UTF-8")
  2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. knitr:::process_file(text, output)
  8. knitr:::process_group.block(group)
  9. knitr:::call_block(x)
     ...
 13. knitr (local) engine(options)
 14. reticulate::eng_python(options)
 15. reticulate:::py_compile_eval(snippet, compile_mode)
 18. builtins$eval(compiled, globals, locals)
 19. reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)

Exécution arrêtée

So I guess you want to see ! ValueError: inside the document - is that right ?

This is using dev knitr and recent rmarkdown and reticulate packages

> packageVersion('reticulate')
[1] ‘1.34.0’
> packageVersion('knitr')
[1] ‘1.45.9’
> packageVersion('rmarkdown')
[1] ‘2.25’

You'll have to add backticks for the chunk, I couldn't get the markdown preview to be right.

BTW about this, you can read how to in the guide : https://yihui.org/issue/#please-format-your-issue-correctly

cderv commented 5 months ago

python engine is handled by reticulate so I wonder if something has changed in there error handling maybe 🤔

brry commented 5 months ago

Thanks for the backtick reference - I somehow missed that section. I fixed that above now.
Yes, the idea is to see ValueError:inside the document.
Oh, I should have thought of reticulate - that is indeed where the change is. Version 1.28 still contained ValueError, Version 1.29 no longer does that. I will investigate and (hopefully^^) solve the issue there.