-
Notifications
You must be signed in to change notification settings - Fork 64
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
.envir
parameter treated differently between glue()
and glue_data()
#308
Comments
Interesting. ## This crashes:
glue::glue_data("{country} is the best!", .envir = list(country = "Portugal"))
#> Error in new.env(hash = hash, parent = parent, size = size): 'enclos' must be an environment
## But this works:
glue::glue_data(.x = NULL, "{country} is the best!", .envir = list(country = "Portugal"))
#> Portugal is the best! Created on 2023-09-15 with reprex v2.0.2 |
Wait this is not the error you think it is - it's expected behavior!
By not explicitly setting glue_data(.x = "{country} is the best!", character(0), .envir = list(country = "Portugal")) The implied This eventually leads to parent_env <- list2env(as.list("{country} is the best!"), parent = list(country = "Portugal"))
#> Error in new.env(hash = hash, parent = parent, size = size): 'enclos' must be an environment Created on 2023-09-15 with reprex v2.0.2 |
Okay but the behaviour is still inconsistent in the way that you've described: library(dplyr)
library(glue)
library(rlang)
set.seed(123)
lil_iris <- slice_sample(iris, n = 3)
## You can specify .envir as a list when you use 'glue'
## (under the hood this is glue_data w/ .x = NULL)
glue("I love {country}!", .envir = list(country = "Portugal"))
#> I love Portugal!
## You can use an actual environment in conjunction with glue_data
glue_data(.x = lil_iris,
"This {Species} has petal width {Petal.Width}. Also, I love {country}!",
.envir = rlang::new_environment(data = list(country = "Portugal"))
)
#> This setosa has petal width 0.1. Also, I love Portugal!
#> This setosa has petal width 0.2. Also, I love Portugal!
#> This virginica has petal width 2.2. Also, I love Portugal!
## You can't use glue_data with .envir as a list
glue_data(.x = lil_iris,
"This {Species} has petal width {Petal.Width}. Also, I love {country}!",
.envir = list(country = "Portugal")
)
#> Error in new.env(hash = hash, parent = parent, size = size): 'enclos' must be an environment Created on 2023-09-15 with reprex v2.0.2 This is happening because when you specify parent_env <- list2env(as.list(.x), parent = .envir) A list cannot be a parent environment without first being converted into a list, so the function crashes. If It's unclear to me whether accepting lists for
|
I think it's very uncommon to use If anything, the right solution is probably just to throw a better error message, i.e. to indicate that |
|
Unconditionally checking that .envir is an environment leads to breakage in revdeps. And I'm not convinced those revdeps are doing anything wrong. This code suggests that glue_data() is designed to accept a non-environment as .envir, in some cases: ``` parent_env <- list2env(as.list(.x), parent = .envir) ```
From the help page of both functions it seems
.envir
parameter ought to be treated in the same way. Butglue::glue_data()
seems to be picky and won't slurp lists.The text was updated successfully, but these errors were encountered: