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

expand_grid with tibbles fails depending on values #1116

Closed
djbirke opened this issue Apr 27, 2021 · 2 comments · Fixed by #1230
Closed

expand_grid with tibbles fails depending on values #1116

djbirke opened this issue Apr 27, 2021 · 2 comments · Fixed by #1230
Labels
bug an unexpected problem or unintended behavior grids #️⃣ expanding, nesting, crossing, ...

Comments

@djbirke
Copy link

djbirke commented Apr 27, 2021

I am trying to create a tibble that contains all combinations of its elements, except for some nesting. In doing so, I encountered the following sensitivity of expand_grid with respect to its contents.

Is this a bug?

tidyr::expand_grid(
  dplyr::tibble(
    fruit = c("Apple", "Banana"),
    fruit_id = c("a", "b")
  ),
  dplyr::tibble(
    status_id = c("c", "d"),
    status = c("cut", "devoured")
  )
)
#> # A tibble: 4 x 4
#>   fruit  fruit_id status_id status  
#>   <chr>  <chr>    <chr>     <chr>   
#> 1 Apple  a        c         cut     
#> 2 Apple  a        d         devoured
#> 3 Banana b        c         cut     
#> 4 Banana b        d         devoured

tidyr::expand_grid(
  dplyr::tibble(
    fruit = c("Apple", "Banana"),
    fruit_id = c("a", "b")
  ),
  dplyr::tibble(
    status_id = c("c", "d"),
    status = c("cut_neatly", "devoured"),
  )
)
#> Error: Column name `dplyr::tibble(...)` must not be duplicated.
#> Use .name_repair to specify repair.

Created on 2021-04-27 by the reprex package (v1.0.0)

@DavisVaughan
Copy link
Member

Seems like a tidyr issue. So I'll move it there.

Related to:
#1092
r-lib/rlang#1116

@DavisVaughan DavisVaughan transferred this issue from tidyverse/dplyr Apr 27, 2021
@DavisVaughan
Copy link
Member

A quick fix is to assign the inlined tibbles to variables, then call expand_grid() with the variable names.

library(tidyr)

df1 <- dplyr::tibble(
  fruit = c("Apple", "Banana"),
  fruit_id = c("a", "b")
)

df2 <- dplyr::tibble(
  status_id = c("c", "d"),
  status = c("cut_neatly", "devoured"),
)

expand_grid(df1, df2)
#> # A tibble: 4 x 4
#>   fruit  fruit_id status_id status    
#>   <chr>  <chr>    <chr>     <chr>     
#> 1 Apple  a        c         cut_neatly
#> 2 Apple  a        d         devoured  
#> 3 Banana b        c         cut_neatly
#> 4 Banana b        d         devoured

Created on 2021-04-27 by the reprex package (v1.0.0)

Sensitivity to input comes from the fact that quos_auto_name() truncates the expressions inside the tibble() call to ... if they are long enough, and in the first example one is long enough but the other isn't, so they look different and everything works fine. In the second example, both get truncated and look the same.

library(rlang)

quos_auto_name(quos(
  dplyr::tibble(
    fruit = c("Apple", "Banana"),
    fruit_id = c("a", "b")
  ),
  dplyr::tibble(
    status_id = c("c", "d"),
    status = c("cut", "devoured")
  )
))
#> <list_of<quosure>>
#> 
#> $`dplyr::tibble(...)`
#> <quosure>
#> expr: ^dplyr::tibble(fruit = c("Apple", "Banana"), fruit_id = c("a", "b"))
#> env:  global
#> 
#> $`dplyr::tibble(status_id = c("c", "d"), status = c("cut", "devoured"))`
#> <quosure>
#> expr: ^dplyr::tibble(status_id = c("c", "d"), status = c("cut", "devoured"))
#> env:  global

quos_auto_name(quos(
  dplyr::tibble(
    fruit = c("Apple", "Banana"),
    fruit_id = c("a", "b")
  ),
  dplyr::tibble(
    status_id = c("c", "d"),
    status = c("cut_neatly", "devoured"),
  )
))
#> <list_of<quosure>>
#> 
#> $`dplyr::tibble(...)`
#> <quosure>
#> expr: ^dplyr::tibble(fruit = c("Apple", "Banana"), fruit_id = c("a", "b"))
#> env:  global
#> 
#> $`dplyr::tibble(...)`
#> <quosure>
#> expr: ^dplyr::tibble(status_id = c("c", "d"), status = c("cut_neatly",
#>           "devoured"), )
#> env:  global

Created on 2021-04-27 by the reprex package (v1.0.0)

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 grids #️⃣ expanding, nesting, crossing, ...
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants