From 3a4e3ae2ea8d0412008bfe07169d3ce5516d53b8 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Tue, 18 Oct 2022 12:05:00 -0600 Subject: [PATCH 1/2] First pass at `join_by` utils --- R/utils-join_by.R | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 R/utils-join_by.R diff --git a/R/utils-join_by.R b/R/utils-join_by.R new file mode 100644 index 00000000..c273af9e --- /dev/null +++ b/R/utils-join_by.R @@ -0,0 +1,59 @@ +# Testing +# join_spec <- join_by(x, y >= z) +# prep_join_by(join_spec) + +# TODO (working list) +# * Move join_by to separate file +# * Renaming columns pre-join? +# * Review dplyr semantics with keep = FALSE & keep = TRUE +# * What to do with between/overlaps/etc. + +# Need to export/create separate file +join_by <- function(...) { + dots <- enquos(...) + dots <- map(dots, quo_squash) + + set_class(dots, c("tidytable_join_by", "list")) +} + +is_join_by <- function(x) { + inherits(x, "tidytable_join_by") +} + +prep_join_by <- function(x, type = "left") { + if (type == "left") { + x <- flip_inputs(x) + } + on <- call2(".", !!!x) + on +} + +flip_inputs <- function(x) { + map(x, flip_input) +} + +flip_input <- function(x) { + if (is_call(x, c("==", "<=", ">=", ">", "<"))) { + out <- flip_operator(x) + out[[3]] <- x[[2]] + out[[2]] <- x[[3]] + out + } else { + out <- x + } + out +} + +flip_operator <- function(x) { + old_operator <- as.character(x[[1]]) + new_operator <- case_match( + old_operator, + "<=" ~ ">=", + ">=" ~ "<=", + ">" ~ "<", + "<" ~ ">", + .default = old_operator + ) + x[[1]] <- sym(new_operator) + x +} From 7d7f870a976f80db9bce7380e387ac6d19d28079 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Fri, 4 Nov 2022 09:48:47 -0600 Subject: [PATCH 2/2] Rerun checks