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

Hidden NULL-column from tibble::new_tibble() causes error in vctrs::vec_rbind() #1834

Closed
sda030 opened this issue Apr 22, 2023 · 1 comment
Closed

Comments

@sda030
Copy link

sda030 commented Apr 22, 2023

Not sure whether to place issue here or at tibble, feel free to move.
I am aware that tibble::new_tibble() is for advanced "only for clean inputs" use, and also that there is perhaps some use for this when creating empty list columns, etc. However, found this a bit surprising. P.S. Context for this is that I wanted to start optimizing my code based on a recent post (https://www.tidyverse.org/blog/2023/04/performant-packages/). Thought I could insert an
list(b = if(condition) "insertme") # else NULL => drop column b.

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
x<-list(a=1, b=NULL) %>% tibble::new_tibble()
x
#> # A tibble: 1 × 2
#>       a
#>   <dbl>
#> 1     1
str(x)
#> tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
#>  $ a: num 1
#>  $ b: NULL
y <- tibble::tibble(a=1, b=NULL)
str(y)
#> tibble [1 × 1] (S3: tbl_df/tbl/data.frame)
#>  $ a: num 1

## Errors with bind_rows() 
dplyr::bind_rows(x)
#> Error in `vec_rbind()`:
#> ! Unexpected `NULL`.
#> ℹ In file 'slice.c' at line 322.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
#> Backtrace:
#>     ▆
#>  1. ├─dplyr::bind_rows(x)
#>  2. │ └─vctrs::vec_rbind(!!!dots, .names_to = .id, .error_call = current_env())
#>  3. └─rlang:::stop_internal_c_lib(...)
#>  4.   └─rlang::abort(message, call = call, .internal = TRUE, .frame = frame)

Created on 2023-04-22 with reprex v2.0.2

@sda030 sda030 changed the title Hidden NULL-column creating problems Hidden NULL-column from tibble::new_tibble() causes error in vctrs::vec_rbind() Apr 22, 2023
@DavisVaughan
Copy link
Member

DavisVaughan commented Apr 26, 2023

I think this is expected behavior everywhere:

  • new_tibble() won't do any of the inputs checks
  • vec_rbind() detects that something is weird and errors (much better than a crash). It gives an "internal" error but that isn't too far off, because it really does mean something fairly low level and internal has gone wrong

I think you'd be happy with using vctrs::df_list() before new_tibble(). It recycles inputs and drops NULLs automatically, and is still very fast.

vctrs::df_list(x = 1, y = NULL)
#> $x
#> [1] 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants