Skip to content

vec_slice not falling back to [ for vctrs_list_of types? #2111

@CoryMcCartan

Description

@CoryMcCartan

We (@christopherkenny and I) are implementing an adjacency list vctrs type. We have a custom [ that reindexes the integer "pointers." However, since our type inherits from vctrs_list_of, we can't seem to get vec_slice() to call [.

For example:

> a = print(adj(2, 1)) # two nodes connected with a single edge
<adj[2]>
[1] {2} {1}

> a[2:1] # expected: switching order yields same list
<adj[2]>
[1] {2} {1}

> vec_slice(a, 2:1) # wrong
<adj[2]>
[1] {1} {2}

We tried implementing an identity vec_proxy() per what seemed like a suggestion in the documentation. However, the only thing we got working was the following, which feels extremely non-ideal, though it fixes vec_slice() for us. (We have separate implementations for vec_proxy_equal(), vec_proxy_compare(), etc.)

vec_proxy.adj <- function(x, ...) {
    seq_along(x)
}
vec_restore.adj <- function(x, to, ...) {
    if (is.integer(x)) {
        to[x]
    } else {
        NextMethod()
    }
}

We see #1116 and #1411, which suggest that the vctrs team has some reasons for the current behavior, e.g., not making vec_slice() generic?

Are we missing an easy fix here, or is there a suggested way to make e.g. slicing a tibble with our class have the same behavior as slicing a data frame? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions