-
Notifications
You must be signed in to change notification settings - Fork 195
New nrow_subset_linter #2298
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
New nrow_subset_linter #2298
Changes from 2 commits
9dc8fee
8540655
3790a7b
a44e737
afc20d6
5b91bcd
a566053
7e67062
180eb6f
0d589be
4bf5bb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #' Block usage of `nrow(subset(x, .))` | ||
| #' | ||
| #' Using `nrow(subset(x, condition))` to count the instances where `condition` | ||
| #' applies inefficiently requires doing a full subset of `x` just to | ||
| #' count the number of rows in the resulting subset. | ||
| #' There are a number of equivalent expressions that don't require the full | ||
| #' subset, e.g. `with(x, sum(condition))` (or, more generically, | ||
| #' `with(x, sum(condition, na.rm = TRUE))`). | ||
| #' The same can be said of other versions of this like | ||
| #' `nrow(DT[(condition)])` for subsetting a `data.table` or | ||
| #' `DT %>% filter(condition) %>% nrow()`. | ||
| #' | ||
| #' @examples | ||
| #' # will produce lints | ||
| #' lint( | ||
| #' text = "nrow(subset(x, is_treatment))", | ||
| #' linters = nrow_subset_linter() | ||
| #' ) | ||
| #' | ||
| #' # okay | ||
| #' lint( | ||
| #' text = "with(x, sum(is_treatment, na.rm = TRUE))", | ||
| #' linters = nrow_subset_linter() | ||
| #' ) | ||
| #' | ||
| #' @evalRd rd_tags("nrow_subset_linter") | ||
| #' @seealso [linters] for a complete list of linters available in lintr. | ||
| #' @export | ||
| nrow_subset_linter <- make_linter_from_xpath( | ||
| xpath = " | ||
| //SYMBOL_FUNCTION_CALL[text() = 'subset'] | ||
| /parent::expr | ||
| /parent::expr | ||
| /parent::expr[expr/SYMBOL_FUNCTION_CALL[text() = 'nrow']] | ||
| ", | ||
| lint_message = paste( | ||
| "Use arithmetic to count the number of rows satisfying a condition,", | ||
| "rather than fully subsetting the table and counting the resulting rows.", | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "For example, replace nrow(subset(x, is_treatment))", | ||
| "with sum(x$is_treatment). NB: use na.rm = TRUE if `is_treatment` has", | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "missing values." | ||
| ) | ||
| ) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # TODO(michaelchirico): activate this false positive test when below cases are done. | ||
| # test_that("nrow_subset_linter skips allowed usages", { | ||
| # # nrow can be avoided here (by chaining the expression and using .N), | ||
| # # but the benefit of doing so is not the same as in the other cases. | ||
| # lintr::expect_lint( | ||
| # "nrow(DT[x == y, 1, by = grp])", | ||
| # NULL, | ||
| # nrow_subset_linter | ||
| # ) | ||
| # }) | ||
|
|
||
| test_that("nrow_subset_linter blocks subset() cases", { | ||
| expect_lint( | ||
| "nrow(subset(x, y == z))", | ||
| rex::rex("Use arithmetic to count the number of rows satisfying a condition"), | ||
| nrow_subset_linter() | ||
| ) | ||
|
|
||
| # TODO(michaelchirico): implement this. | ||
| # lintr::expect_lint( | ||
| # "x %>% subset(y == z) %>% nrow()", | ||
| # "Use arithmetic to count the number of rows satisfying a condition", | ||
| # nrow_subset_linter | ||
| # ) | ||
| }) | ||
|
|
||
| # TODO(michaelchirico): implement these. | ||
| # test_that("nrow_subset_linter blocks [ cases", { | ||
| # # data.frame subsetting (NB: replacement doesn't use na.rm = TRUE) | ||
| # lintr::expect_lint( | ||
| # "nrow(x[x$y == x$z, ])", | ||
| # "Use arithmetic to count the number of rows satisfying a condition", | ||
| # nrow_subset_linter | ||
| # ) | ||
|
|
||
| # # data.table subsetting (NB: replacement needs na.rm = TRUE) | ||
| # lintr::expect_lint( | ||
| # "x[y == z, ]", | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # "Use arithmetic to count the number of rows satisfying a condition", | ||
| # nrow_subset_linter | ||
| # ) | ||
| # }) | ||
|
|
||
| # test_that("nrow_subset_linter blocks dplyr::filter() cases", { | ||
| # lintr::expect_lint( | ||
| # "x %>% filter(y == z) %>% nrow()", | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # "Use arithmetic to count the number of rows satisfying a condition", | ||
| # nrow_subset_linter | ||
| # ) | ||
|
|
||
| # lintr::expect_lint( | ||
| # "nrow(dplyr::filter(x, y == z))", | ||
| # "Use arithmetic to count the number of rows satisfying a condition", | ||
| # nrow_subset_linter | ||
| # ) | ||
| # }) | ||
Uh oh!
There was an error while loading. Please reload this page.