Skip to content

Commit

Permalink
Use dt_squash on if_else args (#221)
Browse files Browse the repository at this point in the history
* Use dt_squash on if_else args. Translate ifelse to fifelse. Fixes #220.

* Update tests

* Update relocate docs
  • Loading branch information
markfairbanks authored Mar 8, 2021
1 parent daea473 commit 9c83443
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

* `tally()` is now translated (@mgirlich, #201).

* `ifelse()` is now mapped to `fifelse()` (@markfairbanks, #220).

* `.data` and `.env` pronouns now work inside of `if_else()` (@markfairbanks, #220).

* More tidyr verbs are in the process of being added:

* `drop_na()` (@markfairbanks, #194)
Expand Down
5 changes: 3 additions & 2 deletions R/tidyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dt_eval <- function(x) {
# even when data.table isn't attached
dt_funs <- c(
"copy", "dcast", "melt", "nafill",
"fcase", "fcoalesce", "fintersect", "frank", "frankv", "fsetdiff", "funion",
"fcase", "fcoalesce", "fifelse", "fintersect", "frank", "frankv", "fsetdiff", "funion",
"setcolorder", "setnames"
)
add_dt_wrappers <- function(env) {
Expand Down Expand Up @@ -129,8 +129,9 @@ dt_squash_call <- function(x, env, data, j = TRUE) {
quote(.GRP)
} else if (is_call(x, "cur_group_rows")) {
quote(.I)
} else if (is_call(x, "if_else")) {
} else if (is_call(x, "if_else") || is_call(x, "ifelse")) {
x[[1L]] <- quote(fifelse)
x[-1] <- lapply(x[-1], dt_squash, env, data, j = j)
x
} else if (is_call(x, "n", n = 0)) {
quote(.N)
Expand Down
2 changes: 1 addition & 1 deletion man/relocate.dtplyr_step.Rd

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

24 changes: 19 additions & 5 deletions tests/testthat/test-tidyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,29 @@ test_that("scoped verbs produce nice output", {
dt %>% summarise_all(~ n()) %>% show_query(),
expr(DT[, .(x = .N)])
)
})

test_that("mask if_else/ifelse & coalesce with data.table versions", {
dt <- lazy_dt(data.table(x = 1:5), "DT")

# mask if_else & coalesce with data.table versions, #112
# if_else/ifelse & coalesce to fifelse & fcoalesce, #112
expect_equal(
dt %>% mutate(y = if_else(x < 3, 1, 2)) %>% show_query(),
expr(copy(DT)[, `:=`(y = fifelse(x < 3, 1, 2))])
)
expect_equal(
dt %>% mutate(y = ifelse(x < 3, 1, 2)) %>% show_query(),
expr(copy(DT)[, `:=`(y = fifelse(x < 3, 1, 2))])
)
expect_equal(
dt %>% summarise_all(~if_else(. > 0, -1, 1)) %>% show_query(),
expr(DT[ , .(x = fifelse(x > 0, -1, 1))])
dt %>% mutate(y = coalesce(x, 1)) %>% show_query(),
expr(copy(DT)[, `:=`(y = fcoalesce(x, 1))])
)

# tidyeval works inside if_else, #220
expect_equal(
dt %>% summarise_all(~coalesce(., 1)) %>% show_query(),
expr(DT[ , .(x = fcoalesce(x, 1))])
dt %>% mutate(y = if_else(.data$x < 3, 1, 2)) %>% show_query(),
expr(copy(DT)[, `:=`(y = fifelse(x < 3, 1, 2))])
)
})

Expand Down

0 comments on commit 9c83443

Please sign in to comment.