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

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

Closed
DavisVaughan opened this issue Jul 1, 2021 · 3 comments · Fixed by #902
Closed

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

DavisVaughan opened this issue Jul 1, 2021 · 3 comments · Fixed by #902
Labels
reprex needs a minimal reproducible example
Milestone

Comments

@DavisVaughan
Copy link
Member

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
@krlmlr krlmlr added the bug an unexpected problem or unintended behavior label Jul 2, 2021
@krlmlr krlmlr added this to the 3.1.2 milestone Jul 2, 2021
@krlmlr
Copy link
Member

krlmlr commented Jul 2, 2021

Thanks, this looks reasonable.

@krlmlr krlmlr modified the milestones: 3.1.2, 3.1.3 Jul 17, 2021
@krlmlr
Copy link
Member

krlmlr commented Jul 17, 2021

Does make_tibble() need to hug its inputs?


author: kirill
date: 2021-07-17
output: "reprex::reprex\_document"
title: ok-coqui_reprex.R

library(tibble)

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

xx <- make_tibble(1:3, NULL)

xx
#> # A tibble: 3 x 1
#>       x
#>   <int>
#> 1     1
#> 2     2
#> 3     3

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

Created on 2021-07-17 by the reprex package (v2.0.0)

@krlmlr krlmlr added reprex needs a minimal reproducible example and removed bug an unexpected problem or unintended behavior labels Jul 17, 2021
krlmlr added a commit that referenced this issue Jul 17, 2021
- `tibble()` and `tibble_row()` ignore all columns that evaluate to `NULL`, not only those where a verbatim `NULL` is passed (#895, #900).
@github-actions
Copy link
Contributor

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
reprex needs a minimal reproducible example
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants