Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vec_proxy_missing() #1928

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ S3method(vec_proxy_equal,array)
S3method(vec_proxy_equal,default)
S3method(vec_proxy_equal,integer64)
S3method(vec_proxy_equal,numeric_version)
S3method(vec_proxy_missing,AsIs)
S3method(vec_proxy_missing,POSIXlt)
S3method(vec_proxy_missing,array)
S3method(vec_proxy_missing,default)
S3method(vec_proxy_order,AsIs)
S3method(vec_proxy_order,array)
S3method(vec_proxy_order,default)
Expand Down Expand Up @@ -597,6 +601,7 @@ export(vec_order)
export(vec_proxy)
export(vec_proxy_compare)
export(vec_proxy_equal)
export(vec_proxy_missing)
export(vec_proxy_order)
export(vec_ptype)
export(vec_ptype2)
Expand Down
3 changes: 1 addition & 2 deletions R/equal.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#' Returns a proxy object (i.e. an atomic vector or data frame of atomic
#' vectors). For [vctr]s, this determines the behaviour of `==` and
#' `!=` (via [vec_equal()]); [unique()], [duplicated()] (via
#' [vec_unique()] and [vec_duplicate_detect()]); [is.na()] and [anyNA()]
#' (via [vec_detect_missing()]).
#' [vec_unique()] and [vec_duplicate_detect()])
#'
#' The default method calls [vec_proxy()], as the default underlying
#' vector data should be equal-able in most cases. If your class is
Expand Down
37 changes: 37 additions & 0 deletions R/missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,43 @@
#' df
NULL

#' Missing proxy
#'
#' Returns a proxy object (i.e. an atomic vector or data frame of atomic
#' vectors). For [vctr]s, this determines the behaviour of
#' [is.na()] and [anyNA()] (via [vec_detect_missing()]).
#'
#' The default method calls [vec_proxy_equal()], as the default
#' equal-able proxy should be used to detect missingness in most cases.
#'
#' @section Data frames:
#' If the proxy for `x` is a data frame, the proxy function is automatically
#' recursively applied on all columns as well. After applying the proxy
#' recursively, if there are any data frame columns present in the proxy, then
#' they are unpacked. Finally, if the resulting data frame only has a single
#' column, then it is unwrapped and a vector is returned as the proxy.
#'
#' @param x A vector x.
#' @inheritParams rlang::args_dots_empty
#'
#' @return A 1d atomic vector or a data frame.
#' @keywords internal
#'
#' @section Dependencies:
#' - [vec_proxy_equal()] called by default
#'
#' @export
vec_proxy_missing <- function(x, ...) {
check_dots_empty0(...)
return(.Call(vctrs_proxy_missing, x))
UseMethod("vec_proxy_missing")
}

#' @export
vec_proxy_missing.default <- function(x, ...) {
stop_native_implementation("vec_proxy_missing.default")
}

#' @rdname missing
#' @export
vec_detect_missing <- function(x) {
Expand Down
6 changes: 6 additions & 0 deletions R/type-asis.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ vec_proxy_equal.AsIs <- function(x, ...) {
vec_proxy_equal(x)
}

#' @export
vec_proxy_missing.AsIs <- function(x, ...) {
x <- asis_strip(x)
vec_proxy_missing(x)
}

#' @export
vec_proxy_compare.AsIs <- function(x, ...) {
x <- asis_strip(x)
Expand Down
11 changes: 11 additions & 0 deletions R/type-bare.R
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@ vec_proxy_equal.array <- function(x, ...) {
vec_proxy_equal(x)
}

# missing --------------------------------------------------------------

#' @export
vec_proxy_missing.array <- function(x, ...) {
# The conversion to data frame is only a stopgap, in the long
# term, we'll hash arrays natively. Note that hashing functions
# similarly convert to data frames.
x <- as.data.frame(x)
vec_proxy_missing(x)
}

# compare ------------------------------------------------------------

#' @export
Expand Down
5 changes: 5 additions & 0 deletions R/type-date-time.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ vec_proxy_equal.POSIXlt <- function(x, ...) {
vec_proxy_equal(x, ...)
}
#' @export
vec_proxy_missing.POSIXlt <- function(x, ...) {
x <- vec_cast(x, new_datetime(tzone = tzone(x)))
vec_proxy_missing(x, ...)
}
#' @export
vec_proxy_compare.POSIXlt <- function(x, ...) {
x <- vec_cast(x, new_datetime(tzone = tzone(x)))
vec_proxy_compare(x)
Expand Down
6 changes: 3 additions & 3 deletions man/theory-faq-coercion.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/vec_proxy_equal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions man/vec_proxy_missing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/complete.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static inline void vec_detect_complete_switch(SEXP x, R_len_t size, int* p_out);

// [[ include("complete.h") ]]
SEXP vec_detect_complete(SEXP x) {
SEXP proxy = PROTECT(vec_proxy_equal(x));
SEXP proxy = PROTECT(vec_proxy_missing(x));

R_len_t size = vec_size(proxy);

Expand Down
11 changes: 11 additions & 0 deletions src/decl/proxy-decl.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
r_obj* syms_vec_proxy;
r_obj* syms_vec_proxy_equal;
r_obj* syms_vec_proxy_equal_array;
r_obj* syms_vec_proxy_missing;
r_obj* syms_vec_proxy_missing_array;
r_obj* syms_vec_proxy_compare;
r_obj* syms_vec_proxy_compare_array;
r_obj* syms_vec_proxy_order;
r_obj* syms_vec_proxy_order_array;

r_obj* fns_vec_proxy_equal_array;
r_obj* fns_vec_proxy_missing_array;
r_obj* fns_vec_proxy_compare_array;
r_obj* fns_vec_proxy_order_array;

Expand All @@ -16,6 +19,8 @@ r_obj* vec_proxy_2(r_obj* x, enum vctrs_recurse recurse);
static inline
r_obj* vec_proxy_equal_impl(r_obj* x);
static inline
r_obj* vec_proxy_missing_impl(r_obj* x);
static inline
r_obj* vec_proxy_compare_impl(r_obj* x);
static inline
r_obj* vec_proxy_order_impl(r_obj* x);
Expand All @@ -26,6 +31,12 @@ r_obj* vec_proxy_equal_method(r_obj* x);
static inline
r_obj* vec_proxy_equal_invoke(r_obj* x, r_obj* method);

static inline
r_obj* vec_proxy_missing_method(r_obj* x);

static inline
r_obj* vec_proxy_missing_invoke(r_obj* x, r_obj* method);

static inline
r_obj* vec_proxy_compare_method(r_obj* x);

Expand Down
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extern r_obj* ffi_vec_restore(r_obj*, r_obj*);
extern r_obj* ffi_vec_restore_recurse(r_obj*, r_obj*);
extern r_obj* ffi_vec_restore_default(r_obj*, r_obj*);
extern SEXP vec_proxy_equal(SEXP);
extern SEXP vec_proxy_missing(SEXP);
extern SEXP vec_proxy_compare(SEXP);
extern SEXP vec_proxy_order(SEXP);
extern r_obj* ffi_df_proxy(r_obj*, r_obj*);
Expand Down Expand Up @@ -245,6 +246,7 @@ static const R_CallMethodDef CallEntries[] = {
{"ffi_vec_proxy", (DL_FUNC) &vec_proxy, 1},
{"ffi_vec_proxy_recurse", (DL_FUNC) &vec_proxy_recurse, 1},
{"vctrs_proxy_equal", (DL_FUNC) &vec_proxy_equal, 1},
{"vctrs_proxy_missing", (DL_FUNC) &vec_proxy_missing, 1},
{"vctrs_proxy_compare", (DL_FUNC) &vec_proxy_compare, 1},
{"vctrs_proxy_order", (DL_FUNC) &vec_proxy_order, 1},
{"ffi_df_proxy", (DL_FUNC) &ffi_df_proxy, 2},
Expand Down
4 changes: 2 additions & 2 deletions src/missing.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ r_obj* ffi_vec_detect_missing(r_obj* x) {

// [[ include("missing.h") ]]
r_obj* vec_detect_missing(r_obj* x) {
r_obj* proxy = KEEP(vec_proxy_equal(x));
r_obj* proxy = KEEP(vec_proxy_missing(x));
r_obj* out = proxy_detect_missing(proxy);
FREE(1);
return out;
Expand Down Expand Up @@ -269,7 +269,7 @@ bool vec_any_missing(r_obj* x) {
}

r_ssize vec_first_missing(r_obj* x) {
r_obj* proxy = KEEP(vec_proxy_equal(x));
r_obj* proxy = KEEP(vec_proxy_missing(x));
r_ssize out = proxy_first_missing(proxy);
FREE(1);
return out;
Expand Down
34 changes: 34 additions & 0 deletions src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ r_obj* vec_proxy_equal(r_obj* x) {
return out;
}

// [[ register() ]]
r_obj* vec_proxy_missing(r_obj* x) {
r_obj* out = KEEP(vec_proxy_missing_impl(x));

if (is_data_frame(out)) {
// Automatically proxy df-proxies recursively.
// Also flattens and unwraps them (#1537, #1664).
out = df_proxy(out, VCTRS_PROXY_KIND_missing);
}

FREE(1);
return out;
}

// [[ register() ]]
r_obj* vec_proxy_compare(r_obj* x) {
r_obj* out = KEEP(vec_proxy_compare_impl(x));
Expand Down Expand Up @@ -111,6 +125,10 @@ r_obj* vec_proxy_equal_impl(r_obj* x) {
VEC_PROXY_KIND_IMPL(vec_proxy_equal_method, vec_proxy_equal_invoke);
}
static inline
r_obj* vec_proxy_missing_impl(r_obj* x) {
VEC_PROXY_KIND_IMPL(vec_proxy_missing_method, vec_proxy_missing_invoke);
}
static inline
r_obj* vec_proxy_compare_impl(r_obj* x) {
VEC_PROXY_KIND_IMPL(vec_proxy_compare_method, vec_proxy_compare_invoke);
}
Expand Down Expand Up @@ -164,6 +182,10 @@ r_obj* vec_proxy_equal_method(r_obj* x) {
return vec_proxy_method_impl(x, "vec_proxy_equal", fns_vec_proxy_equal_array);
}
static inline
r_obj* vec_proxy_missing_method(r_obj* x) {
return vec_proxy_method_impl(x, "vec_proxy_missing", fns_vec_proxy_missing_array);
}
static inline
r_obj* vec_proxy_compare_method(r_obj* x) {
return vec_proxy_method_impl(x, "vec_proxy_compare", fns_vec_proxy_compare_array);
}
Expand Down Expand Up @@ -194,6 +216,10 @@ r_obj* vec_proxy_equal_invoke(r_obj* x, r_obj* method) {
return vec_proxy_invoke_impl(x, method, syms_vec_proxy_equal, vec_proxy);
}
static inline
r_obj* vec_proxy_missing_invoke(r_obj* x, r_obj* method) {
return vec_proxy_invoke_impl(x, method, syms_vec_proxy_missing, &vec_proxy_equal_impl);
}
static inline
r_obj* vec_proxy_compare_invoke(r_obj* x, r_obj* method) {
return vec_proxy_invoke_impl(x, method, syms_vec_proxy_compare, &vec_proxy_equal_impl);
}
Expand Down Expand Up @@ -222,6 +248,7 @@ r_obj* df_proxy(r_obj* x, enum vctrs_proxy_kind kind) {

switch (kind) {
case VCTRS_PROXY_KIND_equal: DF_PROXY(vec_proxy_equal); break;
case VCTRS_PROXY_KIND_missing: DF_PROXY(vec_proxy_missing); break;
case VCTRS_PROXY_KIND_compare: DF_PROXY(vec_proxy_compare); break;
case VCTRS_PROXY_KIND_order: DF_PROXY(vec_proxy_order); break;
}
Expand Down Expand Up @@ -264,25 +291,32 @@ void vctrs_init_data(r_obj* ns) {
syms_vec_proxy_equal = r_sym("vec_proxy_equal");
syms_vec_proxy_equal_array = r_sym("vec_proxy_equal.array");

syms_vec_proxy_missing = r_sym("vec_proxy_missing");
syms_vec_proxy_missing_array = r_sym("vec_proxy_missing.array");

syms_vec_proxy_compare = r_sym("vec_proxy_compare");
syms_vec_proxy_compare_array = r_sym("vec_proxy_compare.array");

syms_vec_proxy_order = r_sym("vec_proxy_order");
syms_vec_proxy_order_array = r_sym("vec_proxy_order.array");

fns_vec_proxy_equal_array = r_env_get(ns, syms_vec_proxy_equal_array);
fns_vec_proxy_missing_array = r_env_get(ns, syms_vec_proxy_missing_array);
fns_vec_proxy_compare_array = r_env_get(ns, syms_vec_proxy_compare_array);
fns_vec_proxy_order_array = r_env_get(ns, syms_vec_proxy_order_array);
}

r_obj* syms_vec_proxy = NULL;
r_obj* syms_vec_proxy_equal = NULL;
r_obj* syms_vec_proxy_equal_array = NULL;
r_obj* syms_vec_proxy_missing = NULL;
r_obj* syms_vec_proxy_missing_array = NULL;
r_obj* syms_vec_proxy_compare = NULL;
r_obj* syms_vec_proxy_compare_array = NULL;
r_obj* syms_vec_proxy_order = NULL;
r_obj* syms_vec_proxy_order_array = NULL;

r_obj* fns_vec_proxy_equal_array = NULL;
r_obj* fns_vec_proxy_missing_array = NULL;
r_obj* fns_vec_proxy_compare_array = NULL;
r_obj* fns_vec_proxy_order_array = NULL;
1 change: 1 addition & 0 deletions src/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

r_obj* vec_proxy(r_obj* x);
r_obj* vec_proxy_equal(r_obj* x);
r_obj* vec_proxy_missing(r_obj* x);
r_obj* vec_proxy_compare(r_obj* x);
r_obj* vec_proxy_order(r_obj* x);
r_obj* vec_proxy_recurse(r_obj* x);
Expand Down
2 changes: 2 additions & 0 deletions src/vctrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ bool vec_is_unspecified(SEXP x);

enum vctrs_proxy_kind {
VCTRS_PROXY_KIND_equal = 0,
VCTRS_PROXY_KIND_missing,
VCTRS_PROXY_KIND_compare,
VCTRS_PROXY_KIND_order
};

SEXP vec_proxy(SEXP x);
SEXP vec_proxy_equal(SEXP x);
SEXP vec_proxy_missing(SEXP x);
SEXP vec_proxy_compare(SEXP x);
SEXP vec_proxy_order(SEXP x);
SEXP vec_proxy_unwrap(SEXP x);
Expand Down
Loading
Loading