Open
Description
Environment
Currently no environment is passed to glue_data()
so the current environment is used as default. This means that the glue string can access internal tidyr data:
us_rent_income %>%
pivot_wider(
names_from = variable,
names_glue = "{variable}_{.value}_{names_vary}",
values_from = c(estimate, moe)
) %>%
names()
#> [1] "GEOID" "NAME"
#> [3] "income_estimate_fastest" "rent_estimate_fastest"
#> [5] "income_moe_fastest" "rent_moe_fastest"
And conversely can't access user data that's not reachable through the global env:
local({
my_string <- "foo"
us_rent_income %>%
pivot_wider(
names_from = variable,
names_glue = "{variable}_{.value}_{my_string}",
values_from = c(estimate, moe)
)
})
#> Error:
#> ! object 'my_string' not found
Englueing
@charliejhadley brought up the following problem a few months ago: https://gist.github.com/charliejhadley/47f123256d392b569fc3c33ec3d436ac
It could be solved by using rlang::englue()
instead of glue::glue_data()
. This would require r-lib/rlang#1565. Then the solution would be:
fn <- function(data, target_column){
data %>%
pivot_wider(
names_from = {{ target_column }},
names_glue = "{{ target_column }}_{.value}",
values_from = c(estimate, moe)
)
}
us_rent_income %>% fn(variable)