Skip to content

assertive programming: tidy stopifnot #7743

@AnthonyEbert

Description

@AnthonyEbert

A tidy version of stopifnot which respects groups

stop_if_not <- function(.data, ...){
  
  statements <- rlang::quos(...)
  
  dplyr::mutate(.data, stopifnot(!!!statements))
  
  return(.data)
}

Example

iris |> group_by(Species) |> stop_if_not(n() == 50)
# Passes

iris |> group_by(Species) |> stop_if_not(n() != 50)
# Fails

starwars |> stop_if_not(n_distinct(name) == n()) 
# Passes

starwars |> stop_if_not(if_all(where(is.numeric), ~ .x > 0))
# Fails

starwars |> stop_if_not(if_all(where(is.numeric), ~ .x > 0 | is.na(.x)))
# Passes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions