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

defer() run immediately when used inside Rmd #186

Closed
hadley opened this issue Jan 4, 2022 · 1 comment
Closed

defer() run immediately when used inside Rmd #186

hadley opened this issue Jan 4, 2022 · 1 comment

Comments

@hadley
Copy link
Member

hadley commented Jan 4, 2022

withr::defer(print("Hi!"))
#> Setting deferred event(s) on global environment.
#>   * Will be run automatically when session ends
#>   * Execute (and clear) with `withr::deferred_run()`.
#>   * Clear (without executing) with `withr::deferred_clear()`.
#> [1] "Hi!"
1
#> [1] 1
2
#> [1] 2

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

@hadley
Copy link
Member Author

hadley commented Jan 4, 2022

Reprex that's more amenable for interactive exploration and that maybe hints at the underlying root cause:

evaluate::replay(evaluate::evaluate("
  withr::defer(print('Hi!'))
  1
"))
#> > 
#> >   withr::defer(print('Hi!'))
#> Setting deferred event(s) on global environment.
#>   * Will be run automatically when session ends
#>   * Execute (and clear) with `withr::deferred_run()`.
#>   * Clear (without executing) with `withr::deferred_clear()`.
#> [1] "Hi!"
#> >   1
#> [1] 1

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

No, that doesn't really illustrate the problem as the handlers are still run once you actually terminate the global environment.

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

No branches or pull requests

1 participant