tibble
tibble copied to clipboard
Implement and use bracket helpers
Variant 3 of https://rpubs.com/krlmlr/607538, with optional variant 2.
Reference: https://github.com/tidyverse/dplyr/issues/5161#issuecomment-621879379.
Also need to check empty dots in methods, no idea why I added them.
After writing a [
method for rsample, I firmly believe that a helper that would return i
and j
in a standardized format would be extremely useful. It is incredibly hard to decipher how many args were provided, which is missing, what to do if drop
is used, etc.
`[.rset` <- function(x, i, j, drop = FALSE, ...) {
out <- NextMethod()
if (missing(i)) {
i <- NULL
} else if (is.null(i)) {
i <- integer()
}
if (missing(j)) {
j <- NULL
} else if (is.null(j)) {
j <- integer()
}
n_real_args <- nargs() - !missing(drop)
if (n_real_args <= 2L) {
j <- i
i <- NULL
}
# Row subset of some kind.
# rset structure must be lost here.
if (!is.null(i)) {
out <- rset_strip(out)
return(out)
}
# Both `i` and `j` are `NULL` (i.e. both are missing)
# This happens with `x[]` and it means we select everything.
if (is.null(j)) {
return(out)
}
# Otherwise, only `j` is used, so we are column subsetting.
# Check that `splits` and each `id` column still
# exist in `out` exactly once.
if (col_subset_requires_fallback(x, out)) {
out <- rset_strip(out)
}
out
}
@krlmlr i spent some time working on this and came up with: https://github.com/DavisVaughan/standardize
This looks great. Can we move this to vctrs at some point?