tibble icon indicating copy to clipboard operation
tibble copied to clipboard

Implement and use bracket helpers

Open krlmlr opened this issue 4 years ago • 3 comments

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.

krlmlr avatar May 01 '20 13:05 krlmlr

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
}

DavisVaughan avatar May 07 '20 15:05 DavisVaughan

@krlmlr i spent some time working on this and came up with: https://github.com/DavisVaughan/standardize

DavisVaughan avatar May 08 '20 23:05 DavisVaughan

This looks great. Can we move this to vctrs at some point?

krlmlr avatar May 09 '20 05:05 krlmlr