Skip to content

Commit d221cf8

Browse files
Merge 43dbae4 into ca1b16d
2 parents ca1b16d + 43dbae4 commit d221cf8

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Collate:
141141
'nested_ifelse_linter.R'
142142
'nested_pipe_linter.R'
143143
'nonportable_path_linter.R'
144+
'shared_constants.R'
144145
'nrow_subset_linter.R'
145146
'numeric_leading_zero_linter.R'
146147
'nzchar_linter.R'
@@ -173,7 +174,6 @@ Collate:
173174
'seq_linter.R'
174175
'settings.R'
175176
'settings_utils.R'
176-
'shared_constants.R'
177177
'sort_linter.R'
178178
'source_utils.R'
179179
'spaces_inside_linter.R'

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
* `consecutive_mutate_linter()` for encouraging consecutive calls to `dplyr::mutate()` to be combined (part of #884, @MichaelChirico).
6060
* `if_switch_linter()` for encouraging `switch()` over repeated `if`/`else` tests (part of #884, @MichaelChirico).
6161
* `nested_pipe_linter()` for discouraging pipes within pipes, e.g. `df1 %>% inner_join(df2 %>% select(a, b))` (part of #884, @MichaelChirico).
62-
* `nrow_subset_linter()` for discouraging usage like `nrow(subset(x, conditions))` in favor of something like `with(x, sum(conditions))` which doesn't require a full subset of `x` (part of #884, @MichaelChirico).
62+
* `nrow_subset_linter()` for discouraging usage like `nrow(subset(x, conditions))` in favor of something like `with(x, sum(conditions))` which doesn't require a full subset of `x` (#2314 and part of #884, @MichaelChirico).
6363
* `pipe_return_linter()` for discouraging usage of `return()` inside a {magrittr} pipeline (part of #884, @MichaelChirico).
6464
* `one_call_pipe_linter()` for discouraging one-step pipelines like `x |> as.character()` (#2330 and part of #884, @MichaelChirico).
6565
* `object_overwrite_linter()` for discouraging re-use of upstream package exports as local variables (#2344, #2346 and part of #884, @MichaelChirico and @AshesITR).

R/nrow_subset_linter.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@
2222
#'
2323
#' @evalRd rd_tags("nrow_subset_linter")
2424
#' @seealso [linters] for a complete list of linters available in lintr.
25+
#' @include shared_constants.R
2526
#' @export
2627
nrow_subset_linter <- make_linter_from_function_xpath(
2728
function_names = "subset",
28-
xpath = "
29+
xpath = glue("
2930
parent::expr
3031
/parent::expr
31-
/parent::expr[expr/SYMBOL_FUNCTION_CALL[text() = 'nrow']]
32-
",
32+
/parent::expr[
33+
expr/SYMBOL_FUNCTION_CALL[text() = 'nrow']
34+
or (self::expr | parent::expr)[
35+
(PIPE or SPECIAL[{ xp_text_in_table(setdiff(magrittr_pipes, c('%$%', '%<>%'))) }])
36+
and expr/expr/SYMBOL_FUNCTION_CALL[text() = 'nrow']
37+
]
38+
]
39+
"),
3340
lint_message = paste(
3441
"Use arithmetic to count the number of rows satisfying a condition,",
3542
"rather than fully subsetting the data.frame and counting the resulting rows.",
36-
"For example, replace nrow(subset(x, is_treatment))",
37-
"with sum(x$is_treatment). NB: use na.rm = TRUE if `is_treatment` has",
38-
"missing values."
43+
"For example, replace nrow(subset(x, is_treatment)) with sum(x$is_treatment).",
44+
"NB: use na.rm = TRUE if `is_treatment` has missing values."
3945
)
4046
)

tests/testthat/test-nrow_subset_linter.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ test_that("lints vectorize", {
2929
nrow_subset_linter()
3030
)
3131
})
32+
33+
test_that("linter is pipeline-aware", {
34+
linter <- nrow_subset_linter()
35+
lint_msg <- "Use arithmetic to count the number of rows satisfying a condition"
36+
37+
expect_lint("x %>% subset(y == z) %>% nrow()", lint_msg, linter)
38+
expect_lint("subset(x) %>% nrow()", lint_msg, linter)
39+
40+
skip_if_not_r_version("4.1.0")
41+
expect_lint("x |> subset(y == z) |> nrow()", lint_msg, linter)
42+
})

0 commit comments

Comments
 (0)