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

with_package is detaching the package if it was attached before call #107

Closed
moodymudskipper opened this issue Jun 26, 2019 · 4 comments
Closed
Labels
feature a feature request or enhancement

Comments

@moodymudskipper
Copy link

I believe it's not expected :

reprex :

library(glue)
x <- "hello"
glue("{x} world")
#> hello world
withr::with_package("glue", glue("{x} world"))
#> hello world
glue("{x} world")
#> Error in glue("{x} world"): could not find function "glue"

Created on 2019-06-26 by the reprex package (v0.3.0)

@jimhester
Copy link
Member

If the package is already attached there isn't much point in using with_package() though...

@moodymudskipper
Copy link
Author

Sure, but it could used in a function, and the user doesn't call it directly and doesn't know that it has been called, and then the function would have a mysterious side effect.

@jonathan-g
Copy link

I ran into this issue with a package I'm writing that will allow the user to specify packages to be temporarily loaded when calling knitr::knit_expand() to expand R macros in a template.

Code like that in #138 is a simple workaround, so there's no reason to change the code in withr:

if (package %in% .packages()) {
  eval(code)
} else {
  withr::with_package(package, code)
}

But it might be nice to mention the side effect in the documentation for withr::withr, which currently says

Upon completion or error, the global environment is restored to the previous state.

@mllg
Copy link
Member

mllg commented Jan 13, 2020

+1 for only detaching the package if it has been attached by withr. The package description says "Methods For Temporarily Modifying Global State", but with_package() is performing a permanent global state change.

@jimhester jimhester added the feature a feature request or enhancement label Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants