-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New nrow_subset_linter * examples * remove commented code * documentation feedback * remove 'readability' tag * more tests
- Loading branch information
1 parent
c96d024
commit ff1dc21
Showing
11 changed files
with
118 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#' 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))`). | ||
#' | ||
#' @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 data.frame and counting the resulting rows.", | ||
"For example, replace nrow(subset(x, is_treatment))", | ||
"with sum(x$is_treatment). NB: use na.rm = TRUE if `is_treatment` has", | ||
"missing values." | ||
) | ||
) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
test_that("nrow_subset_linter skips allowed usage", { | ||
linter <- nrow_subset_linter() | ||
|
||
expect_lint("nrow(foo(subset(x, y == z)))", NULL, linter) | ||
expect_lint("with(x, sum(y == z))", NULL, 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() | ||
) | ||
}) | ||
|
||
test_that("lints vectorize", { | ||
lint_msg <- rex::rex("Use arithmetic to count the number of rows satisfying a condition") | ||
|
||
expect_lint( | ||
trim_some("{ | ||
nrow(subset(x, y == z)) | ||
subset(x) %>% transform(m = 2) | ||
nrow(subset(a, b == c)) | ||
}"), | ||
list( | ||
list(lint_msg, line_number = 2L), | ||
list(lint_msg, line_number = 4L) | ||
), | ||
nrow_subset_linter() | ||
) | ||
}) |