socialresearchcentre / projectable

Produce table-like objects made up of special metadata-rich column vectors, and project them into two dimensions.
https://socialresearchcentre.github.io/projectable
GNU General Public License v3.0
3 stars 0 forks source link

Self-coercion of new class definition #33

Open justanna95 opened 1 year ago

justanna95 commented 1 year ago

Came across an issue when trying to row bind projectable columns of the same class that were created with the helper around new_col() along the lines of can't combine elements with different attributes. Fixed the issue by explicitly adding these helper functions where the new class was being defined.

# Helpers ----------------------------------------------------------------------
#' @export
#' @rdname col_median_ci
is_col_median_ci <- function(x) {
  inherits(x, "projectable_col_median_ci")
}

#' @export
`names<-.projectable_col_median_ci` <- function(x, value) {
  names(vctrs::field(x, "median")) <- value
  x
}

#' @export
names.projectable_col_median_ci <- function(x) {
  names(vctrs::field(x, "median"))
}

# Define coercion rules --------------------------------------------------------

# Self-coercion
#' @export
vec_ptype2.projectable_col_median_ci.projectable_col_median_ci <- function(x, y, ...) {
  new_col_median_ci()
}

# Define comparison rules ------------------------------------------------------

#' @export
vec_proxy_compare.projectable_col_median_ci <- function(x, ...) {
  vec_proxy_compare.projectable_col(x, ...)
}

I think only the coercion and comparison rules ones are required, but I haven't tested that.