Skip to content

Commit

Permalink
Merge pull request tidyverse#2859 from lionel-/fix-top-n-2648
Browse files Browse the repository at this point in the history
Implement top_n() lazily
  • Loading branch information
lionel- committed Jun 14, 2017
2 parents 08500a4 + bc56f73 commit 81cef2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# dplyr 0.7.0.9000

* Fix undefined behaviour in `between()`, where `NA_REAL` were assigned instead of `NA_LOGICAL`. (#2855, @zeehio)
* Fix undefined behaviour in `between()`, where `NA_REAL` were
assigned instead of `NA_LOGICAL`. (#2855, @zeehio)

* `top_n()` now executes operations lazily for compatibility with
database backends (#2848).

# dplyr 0.7.0

Expand Down
11 changes: 5 additions & 6 deletions R/top-n.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,19 @@ top_n <- function(x, n, wt) {

if (quo_is_missing(wt)) {
vars <- tbl_vars(x)
inform(glue("Selecting by ", vars[length(vars)]))
wt <- pull(x, -1)
} else {
wt <- eval_tidy(wt, x)
wt_name <- vars[length(vars)]
inform(glue("Selecting by ", wt_name))
wt <- sym(wt_name)
}

if (!is_scalar_integerish(n)) {
abort("`n` must be a scalar integer")
}

if (n > 0) {
quo <- quo(filter(x, min_rank(desc(!!wt)) <= !!n))
quo <- quo(filter(x, min_rank(desc(!! wt)) <= !! n))
} else {
quo <- quo(filter(x, min_rank(!!wt) <= !!abs(n)))
quo <- quo(filter(x, min_rank(!! wt) <= !! abs(n)))
}

eval_tidy(quo)
Expand Down

0 comments on commit 81cef2e

Please sign in to comment.