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

slice() doesn't grab excess rows #275

Merged
merged 5 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@

* Can use `T` to specify the default in `case_when()`, (#272).

* `slice()` no longer returns excess rows (#10).

* More translations for tidyr verbs have been added:

* `drop_na()` (@markfairbanks, #194)
Expand Down
13 changes: 9 additions & 4 deletions R/step-subset-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ slice.dtplyr_step <- function(.data, ...) {

if (length(dots) == 0) {
i <- NULL
} else if (length(dots) == 1) {
i <- dots[[1]]
} else {
i <- call2("c", !!!dots)
if (length(dots) == 1) {
markfairbanks marked this conversation as resolved.
Show resolved Hide resolved
.rows <- dots[[1]]
} else {
.rows <- call2("c", !!!dots)
}
# Update logic once data.table #4353 is merged
# https://github.com/Rdatatable/data.table/pull/4353
between <- call2("between", .rows, quote(-.N), quote(.N))
i <- call2("[", .rows, between)
}
i <- expr(!!i)

step_subset_i(.data, i)
}
Expand Down
2 changes: 1 addition & 1 deletion R/tidyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dt_eval <- function(x) {
# Make sure data.table functions are available so dtplyr still works
# even when data.table isn't attached
dt_funs <- c(
"CJ", "copy", "dcast", "melt", "nafill",
"between", "CJ", "copy", "dcast", "melt", "nafill",
"fcase", "fcoalesce", "fifelse", "fintersect", "frank", "frankv", "fsetdiff", "funion",
"setcolorder", "setnames", "tstrsplit"
)
Expand Down
16 changes: 12 additions & 4 deletions tests/testthat/test-step-subset-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ test_that("can slice", {
expr(DT)
)
expect_equal(
dt %>% slice(1:4) %>% show_query(),
expr(DT[1:4])
dt %>% slice(c(1, 2)) %>% show_query(),
expr(DT[c(1, 2)[between(c(1, 2), -.N, .N)]])
)
expect_equal(
dt %>% slice(1, 2, 3) %>% show_query(),
expr(DT[c(1, 2, 3)])
expr(DT[c(1, 2, 3)[between(c(1, 2, 3), -.N, .N)]])
)
})

Expand All @@ -22,7 +22,7 @@ test_that("can slice when grouped", {

expect_equal(
dt2 %>% show_query(),
expr(DT[DT[, .I[1], by = .(x)]$V1])
expr(DT[DT[, .I[1[between(1, -.N, .N)]], by = .(x)]$V1])
)
expect_equal(as_tibble(dt2), tibble(x = c(1, 2), y = c(1, 3)))
})
Expand All @@ -35,6 +35,14 @@ test_that("slicing doesn't sorts groups", {
)
})

test_that("doesn't return excess rows, #10", {
dt <- lazy_dt(data.table(x = 1:2))
expect_equal(
dt %>% slice(1:3) %>% pull(x),
1:2
)
})

# variants ----------------------------------------------------------------

test_that("functions silently truncate results", {
Expand Down