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

withr not working properly in pkgdown examples #230

Closed
ashbythorpe opened this issue Sep 15, 2023 · 3 comments · Fixed by #243
Closed

withr not working properly in pkgdown examples #230

ashbythorpe opened this issue Sep 15, 2023 · 3 comments · Fixed by #243

Comments

@ashbythorpe
Copy link

In:
https://withr.r-lib.org/reference/defer.html#ref-examples
and:
https://withr.r-lib.org/dev/reference/defer.html#ref-examples

The examples show incorrect output:

# defer and trigger events on the global environment
defer(print("one"))
#> [1] "one"
defer(print("two"))
#> [1] "two"
deferred_run()

defer(print("three"))
#> [1] "three"
deferred_clear()
deferred_run()

The deferred expressions are evaluated immediately, rather than when withr::deferred_run() is called. This behaviour can be reproduced locally by cloning the repository and running devtools::build_site().

I'm experiencing this issue in a package of mine that relies heavily on withr to manage local state. In my experience, examples seem to work fine when run using devtools::run_examples() and inside R CMD CHECK. You can also see the issue occur here:
https://lintr.r-lib.org/reference/linters_with_defaults.html#ref-examples

Is this a known issue, and is there a workaround?

@IndrajeetPatil
Copy link

AFACIT, this is expected behaviour. Here is a relevant section from R Packages book:

Screenshot 2023-09-20 at 12 36 05

I am also not sure how to proceed, and this is also why we haven't been able to fix a documentation issue in {lintr}.

@hadley
Copy link
Member

hadley commented Jan 9, 2024

Yeah, this is the expected behaviour when defer() is called outside of a function (as in examples). But we can make more explicit in this example.

@hadley
Copy link
Member

hadley commented Jan 9, 2024

Ooops, no, this is only the expected behaviour when called from examples. It looks different when called from the global environment.

@lionel- I think the best we can do here is remove that example, since as far as I can there's no easy way to simulate what it'll look like when run manually in the global environment. (e.g. even if you eval specifically in the global environment then it code and results won't be interleaved correctly).

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 a pull request may close this issue.

3 participants