r-lib / vctrs

Generic programming with typed R vectors
https://vctrs.r-lib.org
Other
287 stars 66 forks source link

Inconsistent handling of NAs in `vec_c` for `vctrs_vctr` #1917

Closed fabian-s closed 6 months ago

fabian-s commented 7 months ago

Concatenating a numeric NA to a vector works, anything else does not:

library(vctrs)

new_vecreprex <- function(x = 1){
  new_vctr(as.list(x), class = "vecreprex", 
           meta = "very important info, must be preserved")
}

a <- new_vecreprex(2:3)

c(a, NA) |> str() #great!
## vecreprx [1:3] 
## $ : int 2
## $ : int 3
## $ : NULL
## @ meta: chr "very important info, must be preserved"

c(NA, a) |> str() #fails, loses class attributes etc 
## List of 3
## $ : logi NA
## $ : int 2
## $ : int 3

c(a, NA*1) |> str() #logical NAs are ok but numerical ones are not? seems very weird....
## Error in `vec_c()`:
## ! Can't combine `..1` <vecreprex> and `..2` <double>.

see also https://github.com/tidyfun/tf/issues/5

fabian-s commented 6 months ago

sorry, i misread the docs. It's now clear to me that this is actually the expected behavior for c and that vec_c in contrast does what it should.

if any vctrs developers read this, an important Q the docs could maybe answer more explicitly is how to make sure that vec_c is used for concatenation instead of c as often as possible...?