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

Closed
catalamarti opened this issue Mar 7, 2024 · 2 comments
Closed

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.

@lionel-
Copy link
Member

lionel- commented Oct 24, 2024

This is expected, tidyselection require {{ for passing arguments.

They sort of work in some cases with lazy evaluation (as in your several-eval example). However once the argument is forced it will not be evaluated again in that case.

@lionel- lionel- closed this as not planned Won't fix, can't repro, duplicate, stale Oct 24, 2024
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

2 participants