Can't dispatch methods with vctrs internals to S7 objects
calderonsamuel opened this issue · comments
Samuel Calderon commented
I'm developing on top of {S7}
and finding difficult to dispath methods based on vctrs to be used with S7 data.frames. You can see the initial discussion on the S7 repo here: RConsortium/S7#367
Here is a comparison between a S3 and a S7 child class of data.frame using vec_slice()
, vec_assert()
and obj_check_vector()
.
library(S7)
library(vctrs)
#> Warning: package 'vctrs' was built under R version 4.3.1
foo_S3 <- function(x = data.frame()) {
class(x) <- c("foo_S3", class(x))
x
}
foo_S7 <- new_class("foo_S7", parent = class_data.frame)
base_df <- data.frame(x = seq(5, 1), y = letters[1:5], z = rep(c("A", "B"), times = c(2, 3)))
bar_S3 <- foo_S3(base_df)
bar_S7 <- foo_S7(base_df)
# they both inherit from data.frame
class(bar_S3)
#> [1] "foo_S3" "data.frame"
class(bar_S7)
#> [1] "foo_S7" "data.frame" "S7_object"
vec_slice(bar_S3, 1:3)
#> x y z
#> 1 5 a A
#> 2 4 b A
#> 3 3 c B
vec_slice(bar_S7, 1:3)
#> Error in `vec_slice()`:
#> ! `x` must be a vector, not a <foo_S7/data.frame/S7_object> object.
#> Backtrace:
#> ▆
#> 1. ├─vctrs::vec_slice(bar_S7, 1:3)
#> 2. └─vctrs:::stop_scalar_type(`<fn>`(`<foo_S7[,3]>`), "x", `<env>`)
#> 3. └─vctrs:::stop_vctrs(...)
#> 4. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
vec_assert(bar_S3) # no output
vec_assert(bar_S7)
#> Error:
#> ! `bar_S7` must be a vector, not a <foo_S7/data.frame/S7_object> object.
#> Backtrace:
#> ▆
#> 1. └─vctrs::vec_assert(bar_S7)
#> 2. └─vctrs:::stop_scalar_type(x, arg, call = call)
#> 3. └─vctrs:::stop_vctrs(...)
#> 4. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
obj_check_vector(bar_S3) # no output
obj_check_vector(bar_S7)
#> Error:
#> ! `bar_S7` must be a vector, not a <foo_S7/data.frame/S7_object> object.
#> Backtrace:
#> ▆
#> 1. ├─vctrs::obj_check_vector(bar_S7)
#> 2. └─vctrs:::stop_scalar_type(`<fn>`(`<foo_S7[,3]>`), "bar_S7", `<env>`)
#> 3. └─vctrs:::stop_vctrs(...)
#> 4. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
sessioninfo::session_info(pkgs = "attached")
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.3.0 (2023-04-21 ucrt)
#> os Windows 11 x64 (build 22621)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate Spanish_Peru.utf8
#> ctype Spanish_Peru.utf8
#> tz America/Lima
#> date 2023-09-22
#> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> S7 * 0.1.1 2023-09-17 [1] CRAN (R 4.3.0)
#> vctrs * 0.6.3 2023-06-14 [1] CRAN (R 4.3.1)
#>
#> [1] C:/Users/dgco93/AppData/Local/R/win-library/4.3
#> [2] C:/Program Files/R/R-4.3.0/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
Created on 2023-09-22 with reprex v2.0.2