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

names_glue evaluation issues #1485

Open
lionel- opened this issue Feb 16, 2023 · 3 comments
Open

names_glue evaluation issues #1485

lionel- opened this issue Feb 16, 2023 · 3 comments
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@lionel-
Copy link
Member

lionel- commented Feb 16, 2023

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)
@DavisVaughan
Copy link
Member

In unnest_longer() I remember making a conscious decision to evaluate indices_to and values_to in an empty environment that just knows about {col}

tidyr/R/unnest-longer.R

Lines 290 to 295 in 0764e65

glue_col_names <- function(string, col_names) {
data <- list(col = col_names)
out <- glue::glue_data(data, string, .envir = NULL)
out <- as.character(out)
out
}

I'm not saying that is 100% correct, just that we should consider updating here too if we decide to change anything

@lionel-
Copy link
Member Author

lionel- commented Feb 17, 2023

That's a valid approach as well. It makes it easier to implement but removes some flexibility for users (e.g. varying suffixes/prefixes).

If we want the englue syntax though, we necessarily need the user environment because that's where the embraced function arguments live.

@lionel-
Copy link
Member Author

lionel- commented Mar 13, 2023

bummer, I forgot to relax this:

Error in `rlang::englue()`:
#> ! Must use `{{`.
#> ℹ Use `glue::glue()` for interpolation with `{`.

This prevents englue() from being used in tidyr. I've opened an issue to remember: r-lib/rlang#1601

@hadley hadley added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" labels Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

No branches or pull requests

3 participants