Skip to content

tibble() doesn't always drop NULL columns #900

@DavisVaughan

Description

@DavisVaughan

Generally, tibble() drops NULL input columns

library(tibble)

tibble(x = 1:3, y = NULL)
#> # A tibble: 3 x 1
#>       x
#>   <int>
#> 1     1
#> 2     2
#> 3     3

But if the tibble() call is wrapped up in a function, then the internal usage of quo_is_null() doesn't see the input as a NULL value.

library(tibble)

make_tibble <- function(x, y) {
  tibble(x = x, y = y)
}

xx <- make_tibble(1:3, NULL)

xx
#> Error: Internal error in `df_slice()`: Columns must match the data frame size.

str(xx)
#> tibble [3 × 2] (S3: tbl_df/tbl/data.frame)
#>  $ x: int [1:3] 1 2 3
#>  $ y: NULL

I'm fairly certain an easy fix for this is to switch from quos() to enquos() in tibble(), that should be more appropriate anyways as enquos() is for capturing arguments:

xs <- quos(...)

And here in tibble_row(), might need to look for more usage of it:

xs <- quos(...)

Here is an example of quos vs enquos:

wrap_quos <- function(x, y) {
  rlang::quos(x = x, y = y)
}
wrap_enquos <- function(x, y) {
  rlang::enquos(x = x, y = y)
}

wrap_quos(1:3, NULL)
#> <list_of<quosure>>
#> 
#> $x
#> <quosure>
#> expr: ^x
#> env:  0x7ff0fff0ba70
#> 
#> $y
#> <quosure>
#> expr: ^y
#> env:  0x7ff0fff0ba70

wrap_enquos(1:3, NULL)
#> <list_of<quosure>>
#> 
#> $x
#> <quosure>
#> expr: ^1:3
#> env:  global
#> 
#> $y
#> <quosure>
#> expr: ^NULL
#> env:  empty

Metadata

Metadata

Assignees

No one assigned

    Labels

    reprexneeds a minimal reproducible example

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions