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

Incorrect evaluation of tidyselect expressions inside functions? #348

Open
catalamarti opened this issue Mar 7, 2024 · 1 comment
Open

Comments

@catalamarti
Copy link

catalamarti commented Mar 7, 2024

So for me the following behavior is not expected... Any thought?
For what I've seen the name value inside the function changes its value to the position of the selected columns in the first evaluation. So the second evaluation does not evaluate the expression, instead it selects the same position columns than the first evaluation.
In fact, if you add print(name) inside the function severalEvals, instead of returning the typical warning tidyselect expressions can not be evaluated... It returns name = c(1, 3).

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 4.2.3

x <- tibble(a = 1, b = 1, aa = 1) |>
  select(all_of(starts_with("a")))
y <- tibble(b = 1, a = 1, aa = 1) |>
  select(all_of(starts_with("a")))
list(x, y)
#> [[1]]
#> # A tibble: 1 × 2
#>       a    aa
#>   <dbl> <dbl>
#> 1     1     1
#> 
#> [[2]]
#> # A tibble: 1 × 2
#>       a    aa
#>   <dbl> <dbl>
#> 1     1     1

severalEvals <- function(name) {
  x <- tibble(a = 1, b = 1, aa = 1) |>
    select(all_of(name))
  y <- tibble(b = 1, a = 1, aa = 1) |>
    select(all_of(name))
  list(x, y)
}

severalEvals(starts_with("a"))
#> [[1]]
#> # A tibble: 1 × 2
#>       a    aa
#>   <dbl> <dbl>
#> 1     1     1
#> 
#> [[2]]
#> # A tibble: 1 × 2
#>       b    aa
#>   <dbl> <dbl>
#> 1     1     1

Created on 2024-03-07 with reprex v2.0.2

Is this the expected behavior? Is there a way to get the behavior I was expecting?

@catalamarti
Copy link
Author

catalamarti commented Mar 7, 2024

@MimiYuchenGuo have find that we could solve it with {{:

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 4.2.3

severalEvals <- function(name) {
  x <- tibble(a = 1, b = 1, aa = 1) |>
    select({{name}})
  y <- tibble(b = 1, a = 1, aa = 1) |>
    select({{name}})
  list(x, y)
}

severalEvals(starts_with("a"))
#> [[1]]
#> # A tibble: 1 × 2
#>       a    aa
#>   <dbl> <dbl>
#> 1     1     1
#> 
#> [[2]]
#> # A tibble: 1 × 2
#>       a    aa
#>   <dbl> <dbl>
#> 1     1     1

Created on 2024-03-07 with reprex v2.0.2

But it is still an unexpected behavior for me.

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

No branches or pull requests

1 participant