-
Notifications
You must be signed in to change notification settings - Fork 57
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
Use mutate(.keep = "none")
in transmute()
#483
Open
markfairbanks
wants to merge
10
commits into
main
Choose a base branch
from
simplify-transmute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
729f545
Use `mutate(.keep = "none")`
markfairbanks 34f4284
Use a snapshot
markfairbanks 65470de
Remove most tests as they're now covered by `mutate()` tests
markfairbanks 61b0261
Remove `transmute()` test from wrong file
markfairbanks 15c1af8
Remove `transmute()` test
markfairbanks 1baf2f4
News bullet
markfairbanks 8bb7056
Preserve column order
markfairbanks ec1dd71
Add test about column order
markfairbanks bb9a46b
Get correct column order
markfairbanks dfd6772
Fix test
markfairbanks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,249 +1,26 @@ | ||
test_that("simple calls generate expected translations", { | ||
test_that("works", { | ||
dt <- lazy_dt(data.table(x = 1, y = 1, z = 1), "DT") | ||
|
||
expect_equal( | ||
dt %>% transmute(x) %>% show_query(), | ||
expr(DT[, .(x = x)]) | ||
dt %>% transmute(x) %>% collect(), | ||
dt %>% mutate(x, .keep = "none") %>% collect() | ||
) | ||
}) | ||
|
||
test_that("transmute generates compound expression if needed", { | ||
dt <- lazy_dt(data.table(x = 1, y = 2), "DT") | ||
test_that("empty dots preserves groups", { | ||
dt <- lazy_dt(data.table(x = 1, y = 1, z = 1), "DT") %>% | ||
group_by(y) | ||
|
||
expect_equal( | ||
dt %>% transmute(x2 = x * 2, x4 = x2 * 2) %>% show_query(), | ||
expr(DT[, { | ||
x2 <- x * 2 | ||
x4 <- x2 * 2 | ||
.(x2, x4) | ||
}]) | ||
) | ||
}) | ||
|
||
test_that("allows multiple assignment to the same variable", { | ||
dt <- lazy_dt(data.table(x = 1, y = 2), "DT") | ||
|
||
# when nested | ||
expect_equal( | ||
dt %>% transmute(x = x * 2, x = x * 2) %>% show_query(), | ||
expr(DT[, { | ||
x <- x * 2 | ||
x <- x * 2 | ||
.(x) | ||
}]) | ||
) | ||
|
||
# when not nested | ||
expect_equal( | ||
dt %>% transmute(z = 2, y = 3) %>% show_query(), | ||
expr(DT[, .(z = 2, y = 3)]) | ||
) | ||
}) | ||
|
||
|
||
test_that("groups are respected", { | ||
dt <- lazy_dt(data.table(x = 1), "DT") %>% group_by(x) %>% transmute(y = 2) | ||
|
||
expect_equal(dt$vars, c("x", "y")) | ||
expect_equal( | ||
dt %>% show_query(), | ||
expr(DT[, .(y = 2), keyby = .(x)]) | ||
) | ||
}) | ||
|
||
test_that("grouping vars can be transmuted", { | ||
dt <- lazy_dt(data.table(x = 1), "DT") %>% group_by(x) %>% transmute(x = x + 1, y = 2) | ||
|
||
expect_equal(dt$vars, c("x", "y")) | ||
expect_equal(dt$groups, "x") | ||
expect_equal( | ||
dt %>% show_query(), | ||
expr(copy(DT)[, `:=`(x = x + 1)][, .(y = 2), keyby = .(x)]) | ||
) | ||
|
||
skip("transmuting grouping vars with nesting is not supported") | ||
dt <- lazy_dt(data.table(x = 1), "DT") %>% | ||
group_by(x) %>% | ||
transmute(x = x + 1, y = x + 1, x = y + 1) | ||
|
||
expect_equal(dt$vars, c("x", "y")) | ||
expect_equal( | ||
dt %>% collect(), | ||
tibble(x = 4, y = 3) %>% group_by(x) | ||
) | ||
}) | ||
|
||
test_that("empty transmute works", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
expect_equal(transmute(dt) %>% show_query(), expr(DT[, 0L])) | ||
expect_equal(transmute(dt)$vars, character()) | ||
expect_equal(transmute(dt, !!!list()) %>% show_query(), expr(DT[, 0L])) | ||
|
||
dt_grouped <- lazy_dt(data.frame(x = 1), "DT") %>% group_by(x) | ||
expect_equal(transmute(dt_grouped)$vars, "x") | ||
}) | ||
|
||
test_that("only transmuting groups works", { | ||
dt <- lazy_dt(data.frame(x = 1)) %>% group_by(x) | ||
expect_equal(transmute(dt, x) %>% collect(), dt %>% collect()) | ||
expect_equal(transmute(dt, x)$vars, "x") | ||
}) | ||
|
||
test_that("across() can access previously created variables", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
step <- transmute(dt, y = 2, across(y, sqrt)) | ||
expect_equal( | ||
collect(step), | ||
tibble(y = sqrt(2)) | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
y <- 2 | ||
y <- sqrt(y) | ||
.(y) | ||
}]) | ||
) | ||
}) | ||
|
||
test_that("new columns take precedence over global variables", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
y <- 'global var' | ||
step <- transmute(dt, y = 2, z = y + 1) | ||
expect_equal( | ||
collect(step), | ||
tibble(y = 2, z = 3) | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
y <- 2 | ||
z <- y + 1 | ||
.(y, z) | ||
}]) | ||
) | ||
}) | ||
|
||
# var = NULL ------------------------------------------------------------- | ||
|
||
test_that("var = NULL when var is in original data", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
step <- dt %>% transmute(x = 2, z = x*2, x = NULL) | ||
expect_equal( | ||
collect(step), | ||
tibble(z = 4) | ||
) | ||
expect_equal( | ||
step$vars, | ||
"z" | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
x <- 2 | ||
z <- x * 2 | ||
.(x, z) | ||
}][, `:=`("x", NULL)]) | ||
) | ||
}) | ||
|
||
test_that("var = NULL when var is in final output", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
step <- transmute(dt, y = NULL, y = 3) | ||
expect_equal( | ||
collect(step), | ||
tibble(y = 3) | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
y <- NULL | ||
y <- 3 | ||
.(y) | ||
}]) | ||
) | ||
}) | ||
|
||
test_that("temp var with nested arguments", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
step <- transmute(dt, y = 2, z = y*2, y = NULL) | ||
expect_equal( | ||
collect(step), | ||
tibble(z = 4) | ||
) | ||
expect_equal( | ||
step$vars, | ||
"z" | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
y <- 2 | ||
z <- y * 2 | ||
.(y, z) | ||
}][, `:=`("y", NULL)]) | ||
) | ||
}) | ||
res <- dt %>% transmute() %>% collect() | ||
|
||
test_that("temp var with no new vars added", { | ||
dt <- lazy_dt(data.frame(x = 1), "DT") | ||
step <- transmute(dt, y = 2, y = NULL) | ||
expect_equal( | ||
collect(step), | ||
tibble() | ||
) | ||
expect_equal( | ||
step$vars, | ||
character() | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
y <- 2 | ||
.(y) | ||
}][, `:=`("y", NULL)]) | ||
) | ||
expect_equal(names(res), "y") | ||
}) | ||
|
||
test_that("var = NULL works when data is grouped", { | ||
dt <- lazy_dt(data.frame(x = 1, g = 1), "DT") %>% group_by(g) | ||
test_that("preserves column order", { | ||
dt <- lazy_dt(data.table(x = 1, y = 1), "DT") | ||
|
||
# when var is in original data | ||
step <- dt %>% transmute(x = 2, z = x*2, x = NULL) | ||
expect_equal( | ||
collect(step), | ||
tibble(g = 1, z = 4) %>% group_by(g) | ||
) | ||
expect_equal( | ||
step$vars, | ||
c("g", "z") | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
x <- 2 | ||
z <- x * 2 | ||
.(x, z) | ||
}, keyby = .(g)][, `:=`("x", NULL)]) | ||
) | ||
res <- dt %>% transmute(y, x) %>% collect() | ||
|
||
# when var is not in original data | ||
step <- transmute(dt, y = 2, z = y*2, y = NULL) | ||
expect_equal( | ||
collect(step), | ||
tibble(g = 1, z = 4) %>% group_by(g) | ||
) | ||
expect_equal( | ||
step$vars, | ||
c("g", "z") | ||
) | ||
expect_equal( | ||
show_query(step), | ||
expr(DT[, { | ||
y <- 2 | ||
z <- y * 2 | ||
.(y, z) | ||
}, keyby = .(g)][, `:=`("y", NULL)]) | ||
) | ||
expect_equal(names(res), c("y", "x")) | ||
}) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some columns could have been removed in
mutate
/transmute
withcolnm = NULL
, e.g.So, maybe
intersect()
with the colnames of themutate
beforeselect
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or, could use
any_of()