-
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 4 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,7 @@ | ||
| 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() | ||
| ) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.