Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure on.exit() runs at end of chunk #204

Merged
merged 6 commits into from
Jul 11, 2024
Merged

Ensure on.exit() runs at end of chunk #204

merged 6 commits into from
Jul 11, 2024

Conversation

hadley
Copy link
Member

@hadley hadley commented Jul 8, 2024

Fixes #201

@cderv this has the potential to affect existing Rmds, but I think it's low risk as currently on.exit() runs immediately making it effectively useless, so I can't see why anyone would have called it.

@hadley hadley requested review from lionel- and cderv July 8, 2024 08:18
@hadley
Copy link
Member Author

hadley commented Jul 8, 2024

This also changes the behaviour of return(). Previously:

evaluate::evaluate(c(
  "1 + 1",
  "return()", 
  "2 + 2"
))
#> <evaluation>
#> Source code: 
#>   1 + 1
#> Text output: 
#>   [1] 2
#> Source code: 
#>   return()
#> Text output: 
#>   NULL
#> Source code: 
#>   2 + 2
#> Text output: 
#>   [1] 4

Now:

evaluate::evaluate(c(
  "1 + 1",
  "return()", 
  "2 + 2"
))
#> <evaluation>
#> Source code: 
#>   1 + 1
#> Text output: 
#>   [1] 2
#> Source code: 
#>   return()

That seems like better behaviour to me, but it also has the possibility of affecting existing code.

R/evaluate.R Show resolved Hide resolved
@hadley
Copy link
Member Author

hadley commented Jul 9, 2024

Also worth noting we'll need to update withr to match this behaviour.

R/evaluate.R Show resolved Hide resolved
@hadley
Copy link
Member Author

hadley commented Jul 9, 2024

Just noticed that with this PR,

```{r}
#| error: true
mpg
```

returns

mpg
#> Error in eval(as.call(list(cb)), envir): object 'mpg' not found

@lionel-
Copy link
Member

lionel- commented Jul 10, 2024

Could we zap all error calls where the expression is eval(...)?

@hadley
Copy link
Member Author

hadley commented Jul 10, 2024

@lionel- we're already doing that and it works fine within evaluate itself, but it some how leaks out when used in knitr.

R/conditions.R Show resolved Hide resolved
@hadley
Copy link
Member Author

hadley commented Jul 10, 2024

@lionel- now fixed; I don't understand why stop("1") and 3() would get different calls, but maybe you do?

Copy link
Collaborator

@cderv cderv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the change, it makes on.exit useful, and I don't think it was for now.

@hadley hadley merged commit 2c533a9 into main Jul 11, 2024
14 checks passed
@hadley hadley deleted the chunkwise-on.exit branch July 11, 2024 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get chunkwise on.exit() working
3 participants