Skip to content

Commit 56aa92d

Browse files
committed
feat: approx_equal on lists
1 parent 1978bab commit 56aa92d

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ importFrom(rlang,expr_label)
214214
importFrom(rlang,f_env)
215215
importFrom(rlang,f_rhs)
216216
importFrom(rlang,is_bare_integerish)
217+
importFrom(rlang,is_bare_list)
217218
importFrom(rlang,is_bare_numeric)
218219
importFrom(rlang,is_environment)
219220
importFrom(rlang,is_formula)
@@ -262,6 +263,7 @@ importFrom(vctrs,vec_equal)
262263
importFrom(vctrs,vec_in)
263264
importFrom(vctrs,vec_match)
264265
importFrom(vctrs,vec_order)
266+
importFrom(vctrs,vec_ptype)
265267
importFrom(vctrs,vec_rbind)
266268
importFrom(vctrs,vec_recycle)
267269
importFrom(vctrs,vec_recycle_common)

R/epiprocess-package.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#' @importFrom rlang %||%
3232
#' @importFrom rlang arg_match0
3333
#' @importFrom rlang is_bare_integerish
34+
#' @importFrom rlang is_bare_list
3435
#' @importFrom rlang is_bare_numeric
3536
#' @importFrom tibble is_tibble
3637
#' @importFrom tidyr nest
@@ -44,6 +45,7 @@
4445
#' @importFrom vctrs vec_in
4546
#' @importFrom vctrs vec_match
4647
#' @importFrom vctrs vec_order
48+
#' @importFrom vctrs vec_ptype
4749
#' @importFrom vctrs vec_rbind
4850
#' @importFrom vctrs vec_recycle
4951
#' @importFrom vctrs vec_recycle_common

R/patch.R

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#' give faster computation if `vec1` and `vec2` are data frames.
1313
#'
1414
#' @return logical vector; no nonmissing entries if `na_equal = TRUE`. Behavior
15-
#' may differ from `vec_equal` with non-`NA` `NaN`s involved.
15+
#' may differ from `vec_equal` with non-`NA` `NaN`s involved, or for bare
16+
#' lists that contain named vectors.
1617
approx_equal <- function(vec1, vec2, abs_tol, na_equal, .ptype = NULL, inds1 = NULL, inds2 = NULL) {
1718
# Recycle inds if provided; vecs if not:
1819
common_size <- vec_size_common(
@@ -63,10 +64,25 @@ approx_equal0 <- function(vec1, vec2, abs_tol, na_equal, inds1 = NULL, inds2 = N
6364
approx_equal0(vec1[[col_i]], vec2[[col_i]], abs_tol, na_equal, inds1, inds2)
6465
}))
6566
}
67+
} else if (is_bare_list(vec1)) {
68+
vapply(seq_along(vec1), function(i) {
69+
entry1 <- vec1[[i]]
70+
entry2 <- vec2[[i]]
71+
vec_size(entry1) == vec_size(entry2) &&
72+
# This is inconsistent with vec_equal on named vectors; to be
73+
# consistently inconsistent, we avoid dispatching to vec_equal for bare
74+
# lists even with abs_tol = 0:
75+
identical(vec_ptype(entry1), vec_ptype(entry2)) &&
76+
all(approx_equal0(entry1, entry2, abs_tol, na_equal))
77+
}, logical(1L))
6678
} else {
6779
# XXX No special handling for any other types/situations. Makes sense for
68-
# unclassed atomic things; bare lists and certain vctrs classes might want
69-
# recursion / specialization, though.
80+
# unclassed atomic things; custom classes (e.g., distributions) might want
81+
# recursion / specialization, though. approx_equal0 should probably be an S3
82+
# method. Also, abs_tol == 0 --> vec_equal logic should maybe be either be
83+
# hoisted to approx_equal or we should manually recurse on data frames even
84+
# with abs_tol = 0 when that's faster (might depend on presence of inds*),
85+
# after some inconsistencies are ironed out.
7086
if (!is.null(inds1)) {
7187
vec1 <- vec_slice(vec1, inds1)
7288
vec2 <- vec_slice(vec2, inds2)

man/approx_equal.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)