Skip to content

names repair #1092

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

Closed
ggrothendieck opened this issue Feb 9, 2021 · 3 comments · Fixed by #1230
Closed

names repair #1092

ggrothendieck opened this issue Feb 9, 2021 · 3 comments · Fixed by #1230
Labels
bug an unexpected problem or unintended behavior grids #️⃣ expanding, nesting, crossing, ...

Comments

@ggrothendieck
Copy link

ggrothendieck commented Feb 9, 2021

There is no good option for unnamed columns in crossing. Everything gives something undesirable. You can get what you want if you write your own function but its too much work compared to how easy expand.grid is to use. One possibility is to allow .name.repair to accept a prefix so that one could just write name.repair = "V" or maybe it could just default to that.

library(tidyr)
L <- list(0:1, 0:1, 0:1)
V <- function(x) paste0("V", seq_along(x))
crossing(!!!L, .name_repair = V)

expand.grid(L)
@hadley
Copy link
Member

hadley commented Feb 17, 2021

Reprex of underlying problem:

library(tidyr)
L <- list(0:1, 0:1, 0:1)
crossing(!!!L)
#> Error: Column names `<int>` and `<int>` must not be duplicated.

Created on 2021-02-17 by the reprex package (v1.0.0)

Somewhat related to #1060 — there's a general question of how we should handle unnamed inputs. name_repair isn't a great fit for this problem.

@hadley
Copy link
Member

hadley commented Feb 18, 2021

The root cause here is a little different; rather than naming an unnamed list, it comes via quos_auto_name()'s handling of inline literals.quos_auto_name() is called from dots_cols() which is only used by expand(), crossing(), nesting() and expand_grid() so there the option to make these functions behave differently with unnamed inputs.

That said, I don't think we'd want to be inconsistent with tibble():

L <- list(0:1, 0:1, 0:1)
tibble::tibble(!!!L)
#> Error: Column names `<int>` and `<int>` must not be duplicated.

Created on 2021-02-18 by the reprex package (v1.0.0)

So maybe we need to adjust quos_auto_name()?

@davidskalinder
Copy link

Note that some of the current .name_repair settings can make things worse:

library(tidyr)
L <- list(0:1, 2:3)
crossing(!!!L, .name_repair = "universal")
#> New names:
#> * `<int>` -> .int....1
#> * `<int>` -> .int....2
#> # A tibble: 4 x 2
#>   .int....1 .int....2
#>       <int>     <int>
#> 1         2         2
#> 2         2         3
#> 3         3         2
#> 4         3         3

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

This happens because quos_auto_name() assigns the same name to each element of the unquote-spliced argument list (unlike when arguments are specified one by one):

library(rlang)

good <- quos(1:2, 3:4)
bad <- quos(!!!list(1:2, 3:4))

quos_auto_name(good)
#> <list_of<quosure>>
#> 
#> $`1:2`
#> <quosure>
#> expr: ^1:2
#> env:  global
#> 
#> $`3:4`
#> <quosure>
#> expr: ^3:4
#> env:  global

quos_auto_name(bad)
#> <list_of<quosure>>
#> 
#> $`<int>`
#> <quosure>
#> expr: ^<int: 1L, 2L>
#> env:  empty
#> 
#> $`<int>`
#> <quosure>
#> expr: ^<int: 3L, 4L>
#> env:  empty

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

As a result, when dots_cols() is called with an unquote-spliced list, the inner line of its for loop assigns the same value to all elements of its output each time the loop is run; and so dots_cols() ends up returning a bunch of copies of the last element of the unquote-spliced list argument.

@hadley hadley added bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames labels Aug 23, 2021
@DavisVaughan DavisVaughan added grids #️⃣ expanding, nesting, crossing, ... and removed rectangling 🗄️ converting deeply nested lists into tidy data frames labels Nov 8, 2021
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.

4 participants