Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {cli} error messaging for assert_one_to_one() #427

Merged
merged 16 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,12 @@ assert_list_element <- function(list,
#'
#' @param vars2 Second list of variables
#'
#' @param message string passed to `cli::cli_abort(message)`. When `NULL`, default messaging
#' is used (see examples for default messages). `"dataset_name"` can be used in messaging.
#'
#' @param dataset_name string indicating the label/symbol of the object being checked.
#' Default is `rlang::caller_arg(dataset)`.
#' @inheritParams assert_logical_scalar
#'
#' @return
#' An error if the condition is not meet. The input otherwise.
Expand All @@ -1611,7 +1617,13 @@ assert_list_element <- function(list,
#' @family assertion
#' @export
#'
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
assert_one_to_one <- function(dataset, vars1, vars2) {
assert_one_to_one <- function(dataset,
vars1,
vars2,
dataset_name = rlang::caller_arg(dataset),
message = NULL,
class = "assert_one_to_one",
call = parent.frame()) {
assert_vars(vars1)
assert_vars(vars2)
assert_data_frame(dataset, required_vars = expr_c(vars1, vars2))
Expand All @@ -1621,34 +1633,47 @@ assert_one_to_one <- function(dataset, vars1, vars2) {
group_by(!!!vars1) %>%
filter(n() > 1) %>%
arrange(!!!vars1)



if (nrow(one_to_many) > 0) {
admiraldev_environment$one_to_many <- one_to_many
abort(
paste0(
"For some values of ",
vars2chr(vars1),
" there is more than one value of ",
vars2chr(vars2),
".\nCall `get_one_to_many_dataset()` to get all one to many values."

message <- message %||%
c("For some values of {.val {vars2chr(vars1)}} there is more than one
value of {.val {vars2chr(vars2)}}",
"i" = "Call {.fun get_one_to_many_dataset} to get all one-to-many values."
)

cli::cli_abort(
message = message,
call = call,
class = c(class, "assert-admiraldev")
)
}

many_to_one <- uniques %>%
group_by(!!!vars2) %>%
filter(n() > 1) %>%
arrange(!!!vars2)

if (nrow(many_to_one) > 0) {
admiraldev_environment$many_to_one <- many_to_one
abort(
paste0(
"There is more than one value of ",
vars2chr(vars1),
" for some values of ",
vars2chr(vars2),
".\nCall `get_many_to_one_dataset()` to get all many to one values."

message <- message %||%
c("There is more than one value of {.val {vars2chr(vars1)}} for some
values of {.val {vars2chr(vars2)}}",
"i" = "Call {.fun get_many_to_one_dataset} to get all many-to-one values."
)

cli::cli_abort(
message = message,
call = call,
class = c(class, "assert-admiraldev")
)
}

invisible(dataset)
}

#' Is a Variable in a Dataset a Date or Datetime Variable?
Expand Down
32 changes: 31 additions & 1 deletion man/assert_one_to_one.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading