Skip to content

Commit

Permalink
Merge pull request #1 from edalfon/f-assert-on-grouped-data
Browse files Browse the repository at this point in the history
fix assert on grouped data
  • Loading branch information
edalfon authored Jun 19, 2024
2 parents 9590949 + 580e163 commit 60224a9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: efun
Title: Miscellaneous Functions by E
Version: 0.3.1
Version: 0.3.2
Authors@R:
person(given = "Eduardo",
family = "Alfonso-Sierra",
Expand Down
9 changes: 8 additions & 1 deletion R/assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#'
#' @export
assert <- function(.data, ..., msg = "Assertion does not hold") {
# TODO: change this old approach and embrace more dplyr by something like
# abcd <- dplyr::mutate(.data, ...)
# and then just extract the evaluated conditions to report. This also
# allows multiple conditions, unlike the original approach
condition_eval <- with(.data, ...)
if (all(condition_eval)) {
return(.data)
Expand All @@ -42,7 +46,10 @@ assert <- function(.data, ..., msg = "Assertion does not hold") {
cat(msg)

NULL -> .condition_eval
.assert_fails <- dplyr::mutate(.data, .condition_eval = condition_eval)
.assert_fails <- dplyr::mutate(
.data |> dplyr::ungroup(),
.condition_eval = condition_eval
)
if (rlang::is_interactive()) {
utils::View(.assert_fails) # TODO: should we sample?
} else {
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ test_that("assert works", {

expect_identical(mtcars |> assert(mpg < 34), mtcars)
})

test_that("assert does not fail on grouped data frames", {
expect_error(
object = mtcars |> dplyr::group_by(cyl) |> assert(hp < 150),
regexp = "Assertion does not hold"
)
})

0 comments on commit 60224a9

Please sign in to comment.