Skip to content

Commit

Permalink
fix for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 28, 2023
1 parent a14b606 commit e5f1505
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.8.0.3
Version: 0.8.0.4
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ BUG FIXES
* Fixed issue in `recode_into()` with probably wrong case number printed in the
warning when several recode patterns match to one case.

* Fixed issue in `data_filter()` where functions containing a `=` (e.g. when
naming arguments, like `grepl(pattern, x = a)`) were mistakenly seen as
faulty syntax.

# datawizard 0.8.0

BREAKING CHANGES
Expand Down
54 changes: 31 additions & 23 deletions R/data_match.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,37 @@ data_filter.grouped_df <- function(x, ...) {
tmp <- gsub(">=", "", tmp, fixed = TRUE)
tmp <- gsub("!=", "", tmp, fixed = TRUE)

# Give more informative message to users
# about possible misspelled comparisons / logical conditions
# check if "=" instead of "==" was used?
if (any(grepl("=", tmp, fixed = TRUE))) {
insight::format_error(
"Filtering did not work. Please check if you need `==` (instead of `=`) for comparison."
)
}
# check if "&&" etc instead of "&" was used?
logical_operator <- NULL
if (any(grepl("&&", .fcondition, fixed = TRUE))) {
logical_operator <- "&&"
}
if (any(grepl("||", .fcondition, fixed = TRUE))) {
logical_operator <- "||"
}
if (!is.null(logical_operator)) {
insight::format_error(
paste0(
"Filtering did not work. Please check if you need `",
substr(logical_operator, 0, 1),
"` (instead of `", logical_operator, "`) as logical operator."
# we now want to check whether user used a "=" in the filter syntax. This
# typically indicates that the comparison "==" is probably wrong by using "="
# instead. However, if a function was provided, we indeed may have "=", e.g.
# if the pattern was `data_filter(out, grep("pattern", x = value))`. We thus
# first check if we can identify a function call, and if only continue checking
# when we have no function identified
if (!is.function(try(get(gsub("^(.*?)\\((.*)", "\\1", tmp)), silent = TRUE))) {
# Give more informative message to users
# about possible misspelled comparisons / logical conditions
# check if "=" instead of "==" was used?
if (any(grepl("=", tmp, fixed = TRUE))) {
insight::format_error(
"Filtering did not work. Please check if you need `==` (instead of `=`) for comparison."
)
)
}
# check if "&&" etc instead of "&" was used?
logical_operator <- NULL
if (any(grepl("&&", .fcondition, fixed = TRUE))) {
logical_operator <- "&&"
}
if (any(grepl("||", .fcondition, fixed = TRUE))) {
logical_operator <- "||"
}
if (!is.null(logical_operator)) {
insight::format_error(
paste0(
"Filtering did not work. Please check if you need `",
substr(logical_operator, 0, 1),
"` (instead of `", logical_operator, "`) as logical operator."
)
)
}
}
}
20 changes: 20 additions & 0 deletions tests/testthat/test-data_match.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,23 @@ test_that("data_filter with groups, different ways of dots", {
expect_identical(out1, out2)
expect_identical(out1, out3)
})


test_that("data_filter, slicing works with functions", {
d <- data.frame(
a = c("aa", "a1", "bb", "b1", "cc", "c1"),
b = 1:6,
stringsAsFactors = FALSE
)

rows <- grep("^[A-Za-z][0-9]$", x = d$a)
out1 <- data_filter(d, rows)
out2 <- data_filter(d, grep("^[A-Za-z][0-9]$", x = d$a))

expect_identical(out1, out2)

out3 <- data_filter(iris, (Sepal.Width == 3.0) & (Species == "setosa"))
expect_identical(nrow(out3), 6L)

expect_error(data_filter(iris, (Sepal.Width = 3.0) & (Species = "setosa")))
})

0 comments on commit e5f1505

Please sign in to comment.