knitr icon indicating copy to clipboard operation
knitr copied to clipboard

Julia code chunks with errors cause rendering to fail even with `error=TRUE`

Open paciorek opened this issue 1 year ago • 1 comments

I can use error=TRUE with an R code chunk (or the Quarto style #| error: true), but rendering fails with a Julia code chunk:

---
title: test
engine: knitr
---

```{julia, error=TRUE}
y
```

Here's the error (same thing if I do knit2html("test.qmd").

> rmarkdown::render("test.qmd")


processing file: test.qmd
  |....................................................| 100% [unnamed-chunk-1]Error:
! Error happens in Julia.
UndefVarError: `y` not defined
Stacktrace:
 [1] top-level scope
   @ :0
 [2] eval(m::Module, e::Any)
   @ Core ./boot.jl:385
 [3] macro expansion
   @ /system/linux/lib/R-22.04/4.4.0/x86_64/site-library/JuliaCall/julia/RmdStd.jl:15 [inlined]
 [4] top-level scope
   @ ./none:14
 [5] eval
   @ ./boot.jl:385 [inlined]
 [6] eval_string(x::String)
   @ Main.JuliaCall /system/linux/lib/R-22.04/4.4.0/x86_64/site-library/JuliaCall/julia/setup.jl:203
 [7] docall(call1::Ptr{Nothing})
   @ Main.JuliaCall /system/linux/lib/R-22.04/4.4.0/x86_64/site-library/JuliaCall/julia/setup.jl:176
Backtrace:
  1. rmarkdown::render("test.qmd")
  2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. knitr:::process_file(text, output)
  6. knitr:::process_group(group)
  7. knitr:::call_block(x)
     ...
 13. JuliaCall:::stdout_capture_command(buffer)
 14. base::tryCatch(...)
 15. base (local) tryCatchList(expr, classes, parentenv, handlers)
 16. base (local) tryCatchOne(...)
 17. value[[3L]](cond)

Quitting from lines 7-8 [unnamed-chunk-1] (test.qmd)

paciorek avatar Jan 17 '25 01:01 paciorek

When using julia with knitr, JuliaCall::eng_juliacall() is used. This engine needs to handle options$error to do the right think in case of error in Julia.

It seems right now, it is not. This is to fix upstream at https://github.com/JuliaInterop/JuliaCall

You should open an issue upstream (and you can ping me if they need help implementing this)

For example, in reticulate for the python engine, they do detect when error should be captured. https://github.com/rstudio/reticulate/blob/932589f2417968e1da3312dd0b48dba60c058739/R/knitr-engine.R#L221-L225

Right now this is how it works.

@yihui should we try to catch error for any engine when error = TRUE otherwise ? Possibly here: https://github.com/yihui/knitr/blob/8a37857140a0c1a656d5ff76b9742f6a5c01933c/R/block.R#L158

cderv avatar Jan 17 '25 10:01 cderv