r-lib / vctrs

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

`rbind` errors on vctrs_rcrd objects #1924

Open khusmann opened 3 months ago

khusmann commented 3 months ago

vec_rbind() works fine, but base::rbind() fails because it relies on the ability to [<- out-of-index in order to grow the vctr.

Here's a MWE running v0.6.5:

library(tibble)
library(vctrs)

format.foo <- function(x, ...) {
  format(vec_data(x)$a)
}

x <- new_rcrd(list(a = c("foo", "bar"), b = c("baz", "bling")), class="foo")

vec_rbind(tibble(x), tibble(x))
#> # A tibble: 4 × 1
#>       x
#>   <foo>
#> 1   foo
#> 2   bar
#> 3   foo
#> 4   bar

rbind(tibble(x), tibble(x))
#> Error:
#> ! Can't assign to elements past the end.
#> ℹ Locations 3 and 4 don't exist.
#> ℹ There are only 2 elements.
#> Backtrace:
#>     ▆
#>  1. ├─base::rbind(tibble(x), tibble(x))
#>  2. │ └─base::rbind(deparse.level, ...)
#>  3. │   ├─base::`[<-`(`*tmp*`, ri, value = `<foo>`)
#>  4. │   └─vctrs:::`[<-.vctrs_rcrd`(`*tmp*`, ri, value = `<foo>`)
#>  5. │     └─vctrs::vec_assign(vec_data(x), i, vec_data(value))
#>  6. └─vctrs (local) `<fn>`()
#>  7.   └─vctrs:::stop_subscript_oob(...)
#>  8.     └─vctrs:::stop_subscript(...)
#>  9.       └─rlang::abort(...)

Created on 2024-04-08 with reprex v2.0.2