evaluate icon indicating copy to clipboard operation
evaluate copied to clipboard

`on.exit()` not captured inside of a function

Open hadley opened this issue 4 months ago • 2 comments

f <- function() {
  cat("Hello\n")
  on.exit(cat("Bye\n"))
  stop("Error")
}
evaluate::evaluate("f()")
#> <evaluation>
#> Source code: 
#>   f()
#> Text output: 
#>   Hello
#> Condition: 
#>   Error in f():
#>   Error

Created on 2025-08-04 with reprex v2.1.1

hadley avatar Aug 04 '25 22:08 hadley

This is also present in quarto and rmarkdown see: https://github.com/quarto-dev/quarto/issues/796#issuecomment-3300185465

in a .qmd (quarto render error.qmd):

---
format: html
---

```{r}
#| error: true

f <- function() {
  cat("Hello\n")
  on.exit(cat("Bye\n"))
  stop("Error")
}  

f()
```

return:

Hello

Error in f(): Error

In a .rmd

---
output: html_document
---

```{r}
#| error: true

f <- function() {
  cat("Hello\n")
  on.exit(cat("Bye\n"))
  stop("Error")
}  

f()
```

Will return the same:

Hello

Error in f(): Error

defuneste avatar Sep 22 '25 00:09 defuneste

Thanks for additional info.

To confirm: knitr uses evaluate, rmarkdown uses knitr so it will be affected. Same as quarto which will use knitr under the hood when engine: knitr is the one active.

cderv avatar Sep 22 '25 12:09 cderv