Skip to content

Incorrect evaluation of tidyselect expressions inside functions? #348

Closed as not planned
@catalamarti

Description

@catalamarti

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions