Skip to content

Commit 3ff2209

Browse files
authored
fix for functions (#440)
* fix for functions * comment * this is intentional * styler * disable styler, is intentional
1 parent a14b606 commit 3ff2209

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling and Statistical Transformations
4-
Version: 0.8.0.3
4+
Version: 0.8.0.4
55
Authors@R: c(
66
person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut",
77
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ BUG FIXES
1313
* Fixed issue in `recode_into()` with probably wrong case number printed in the
1414
warning when several recode patterns match to one case.
1515

16+
* Fixed issue in `data_filter()` where functions containing a `=` (e.g. when
17+
naming arguments, like `grepl(pattern, x = a)`) were mistakenly seen as
18+
faulty syntax.
19+
1620
# datawizard 0.8.0
1721

1822
BREAKING CHANGES

R/data_match.R

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -311,29 +311,39 @@ data_filter.grouped_df <- function(x, ...) {
311311
tmp <- gsub(">=", "", tmp, fixed = TRUE)
312312
tmp <- gsub("!=", "", tmp, fixed = TRUE)
313313

314-
# Give more informative message to users
315-
# about possible misspelled comparisons / logical conditions
316-
# check if "=" instead of "==" was used?
317-
if (any(grepl("=", tmp, fixed = TRUE))) {
318-
insight::format_error(
319-
"Filtering did not work. Please check if you need `==` (instead of `=`) for comparison."
320-
)
321-
}
322-
# check if "&&" etc instead of "&" was used?
323-
logical_operator <- NULL
324-
if (any(grepl("&&", .fcondition, fixed = TRUE))) {
325-
logical_operator <- "&&"
326-
}
327-
if (any(grepl("||", .fcondition, fixed = TRUE))) {
328-
logical_operator <- "||"
329-
}
330-
if (!is.null(logical_operator)) {
331-
insight::format_error(
332-
paste0(
333-
"Filtering did not work. Please check if you need `",
334-
substr(logical_operator, 0, 1),
335-
"` (instead of `", logical_operator, "`) as logical operator."
314+
# We want to check whether user used a "=" in the filter syntax. This
315+
# typically indicates that the comparison "==" is probably wrong by using
316+
# a "=" instead of `"=="`. However, if a function was provided, we indeed
317+
# may have "=", e.g. if the pattern was
318+
# `data_filter(out, grep("pattern", x = value))`. We thus first check if we
319+
# can identify a function call, and only continue checking for wrong syntax
320+
# when we have not identified a function.
321+
322+
if (!is.function(try(get(gsub("^(.*?)\\((.*)", "\\1", tmp)), silent = TRUE))) {
323+
# Give more informative message to users
324+
# about possible misspelled comparisons / logical conditions
325+
# check if "=" instead of "==" was used?
326+
if (any(grepl("=", tmp, fixed = TRUE))) {
327+
insight::format_error(
328+
"Filtering did not work. Please check if you need `==` (instead of `=`) for comparison."
336329
)
337-
)
330+
}
331+
# check if "&&" etc instead of "&" was used?
332+
logical_operator <- NULL
333+
if (any(grepl("&&", .fcondition, fixed = TRUE))) {
334+
logical_operator <- "&&"
335+
}
336+
if (any(grepl("||", .fcondition, fixed = TRUE))) {
337+
logical_operator <- "||"
338+
}
339+
if (!is.null(logical_operator)) {
340+
insight::format_error(
341+
paste0(
342+
"Filtering did not work. Please check if you need `",
343+
substr(logical_operator, 0, 1),
344+
"` (instead of `", logical_operator, "`) as logical operator."
345+
)
346+
)
347+
}
338348
}
339349
}

tests/testthat/test-data_match.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,28 @@ test_that("data_filter with groups, different ways of dots", {
320320
expect_identical(out1, out2)
321321
expect_identical(out1, out3)
322322
})
323+
324+
325+
test_that("data_filter, slicing works with functions", {
326+
d <- data.frame(
327+
a = c("aa", "a1", "bb", "b1", "cc", "c1"),
328+
b = 1:6,
329+
stringsAsFactors = FALSE
330+
)
331+
332+
rows <- grep("^[A-Za-z][0-9]$", x = d$a)
333+
out1 <- data_filter(d, rows)
334+
out2 <- data_filter(d, grep("^[A-Za-z][0-9]$", x = d$a))
335+
336+
expect_identical(out1, out2)
337+
338+
out3 <- data_filter(iris, (Sepal.Width == 3.0) & (Species == "setosa"))
339+
expect_identical(nrow(out3), 6L)
340+
341+
# styler: off
342+
expect_error(
343+
data_filter(iris, (Sepal.Width = 3.0) & (Species = "setosa")), # nolint
344+
regex = "Filtering did not work"
345+
)
346+
# styler: on
347+
})

0 commit comments

Comments
 (0)