-
-
Notifications
You must be signed in to change notification settings - Fork 7
Make as card check format #534
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
base: main
Are you sure you want to change the base?
Changes from all commits
2339ff7
86ff3f3
f749cbc
f4f5ec2
eb4d3fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |||||||
| #' | ||||||||
| #' Convert data frames to ARDs of class 'card'. | ||||||||
| #' | ||||||||
| #' | ||||||||
| #' @param x (`data.frame`)\cr | ||||||||
| #' a data frame | ||||||||
| #' | ||||||||
| #' @param check (scalar `logical`)\cr | ||||||||
| #' Whether the input data frame should be checked for standard ARD features | ||||||||
| #' @return an ARD data frame of class 'card' | ||||||||
| #' @export | ||||||||
| #' | ||||||||
|
|
@@ -14,17 +16,34 @@ | |||||||
| #' stat_label = c("N", "Mean"), | ||||||||
| #' stat = c(10, 0.5) | ||||||||
| #' ) |> | ||||||||
| #' as_card() | ||||||||
| as_card <- function(x) { | ||||||||
| #' as_card( check = FALSE) | ||||||||
| #' dplyr::tibble( | ||||||||
| #' variable = "AGE", | ||||||||
| #' stat_name = c("N", "mean"), | ||||||||
| #' stat_label = c("N", "Mean"), | ||||||||
| #' stat = list(10, 0.5), | ||||||||
| #' fmt_fun = replicate(2, list()), | ||||||||
| #' warning = replicate(2, list()), | ||||||||
| #' error = replicate(2, list()) | ||||||||
| #' ) |> | ||||||||
| #' as_card( ) | ||||||||
| as_card <- function(x, check = TRUE) { | ||||||||
| set_cli_abort_call() | ||||||||
|
|
||||||||
| # check in inputs ------------------------------------------------------------ | ||||||||
| check_class(x, cls = "data.frame") | ||||||||
|
|
||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| # convert to class "card" ---------------------------------------------------- | ||||||||
| if (inherits(x, "card")) { | ||||||||
| x | ||||||||
| out <- x | ||||||||
| } else { | ||||||||
| structure(x, class = c("card", class(x))) | ||||||||
| out <- structure(x, class = c("card", class(x))) | ||||||||
| } | ||||||||
| if(check){ | ||||||||
| check_ard_structure(out, column_order = FALSE, method = FALSE, | ||||||||
| error_on_fail = TRUE) | ||||||||
| } | ||||||||
|
|
||||||||
| return(out) | ||||||||
|
|
||||||||
|
Comment on lines
+47
to
+48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,25 +9,34 @@ | |
| #' check whether ordering of columns adheres to to `cards::tidy_ard_column_order()`. | ||
| #' @param method (scalar `logical`)\cr | ||
| #' check whether a `"stat_name"` equal to `"method"` appears in results. | ||
| #' @param error_on_fail (scalar `logical`)\cr | ||
| #' Error if a check is failed? FALSE by default. | ||
| #' @return an ARD data frame of class 'card' (invisible) | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' ard_summary(ADSL, variables = "AGE") |> | ||
| #' dplyr::select(-warning, -error) |> | ||
| #' check_ard_structure() | ||
| check_ard_structure <- function(x, column_order = TRUE, method = TRUE) { | ||
| check_ard_structure <- function(x, column_order = TRUE, method = TRUE, | ||
| error_on_fail = FALSE) { | ||
| set_cli_abort_call() | ||
| check_scalar_logical(method) | ||
| check_scalar_logical(column_order) | ||
| check_scalar_logical(error_on_fail) | ||
|
|
||
| # check class ---------------------------------------------------------------- | ||
| if (!inherits(x, "card")) { | ||
| cli::cli_inform("Object is not of class {.cls card}.") | ||
| .message_or_error("Object is not of class {.cls card}.", error_on_fail, | ||
| envir = environment()) | ||
| } | ||
|
|
||
| # exit if not a data frame --------------------------------------------------- | ||
| if (!inherits(x, "data.frame")) { | ||
| .message_or_error( | ||
| "Object is not of class {.cls data.frame}.", | ||
| error = error_on_fail, | ||
| envir = environment()) | ||
| return(invisible()) | ||
| } | ||
|
|
||
|
|
@@ -39,42 +48,58 @@ check_ard_structure <- function(x, column_order = TRUE, method = TRUE) { | |
| ) |> | ||
| setdiff(names(x)) | ||
| if (!is_empty(missing_variables)) { | ||
| cli::cli_inform("The following columns are not present: {.val {missing_variables}}.") | ||
| .message_or_error( | ||
| "The following columns are not present: {.val {missing_variables}}.", | ||
| error = error_on_fail, | ||
| envir = environment()) | ||
| } | ||
|
|
||
| # check whether AR contains a method stat ------------------------------------ | ||
| if (isTRUE(method)) { | ||
| if (!"method" %in% x$stat_name) { | ||
| cli::cli_inform("Expecting a row with {.code stat_name = 'method'}, but it is not present.") | ||
| .message_or_error( | ||
| "Expecting a row with {.code stat_name = 'method'}, but it is not present.", | ||
| error = error_on_fail, | ||
| envir = environment()) | ||
| } | ||
| } | ||
|
|
||
| # check order of columns ----------------------------------------------------- | ||
| if (isTRUE(column_order)) { | ||
| if (!identical(names(x), names(tidy_ard_column_order(x)))) { | ||
| cli::cli_inform( | ||
| c("The column order is not in the standard order.", | ||
| .message_or_error( | ||
| c( | ||
| "The column order is not in the standard order.", | ||
| i = "Use {.fun cards::tidy_ard_column_order} for standard ordering." | ||
| ) | ||
| ) | ||
| ), | ||
| error = error_on_fail, | ||
| envir = environment()) | ||
| } | ||
| } | ||
|
|
||
| # Check whether expected columns are present --------------------------------- | ||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+80
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is missing something |
||
| # check columns are list columns as expected --------------------------------- | ||
| expected_lst_columns <- | ||
| dplyr::select( | ||
| x, all_ard_groups(), all_ard_variables(), | ||
| any_of(c("stat", "fmt_fun", "warning", "error")) | ||
| ) |> | ||
| # remove group## and variable columns | ||
| dplyr::select(-matches("^group[0-9]$"), -"variable") |> | ||
| dplyr::select(-matches("^group[0-9]$"), -any_of("variable")) |> | ||
| names() | ||
| not_a_lst_columns <- | ||
| x[expected_lst_columns] |> | ||
| dplyr::select(-where(is.list)) |> | ||
| names() | ||
| if (!is_empty(not_a_lst_columns)) { | ||
| cli::cli_inform("The following columns are expected to be list columns: {.val {not_a_lst_columns}}.") | ||
| .message_or_error( | ||
| "The following columns are expected to be list columns: {.val {not_a_lst_columns}}.", | ||
| error = error_on_fail, | ||
| envir = environment() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this needed if default is |
||
| ) | ||
| } | ||
|
|
||
| invisible(x) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -90,3 +90,39 @@ | |||||
|
|
||||||
| ret | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| #' Message or error | ||||||
| #' | ||||||
| #' Either error or message depending on input | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| #' | ||||||
| #' @param msg (scalar `character`)\cr | ||||||
| #' Error message | ||||||
| #' @param error (scalar `logical`)\cr | ||||||
| #' If this should produce an error or a warning. FALSE by default | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| #' @param envir (`environment`)\cr | ||||||
| #' Environment to evaluate the glue expressions in passed in `cli::cli_abort(message)`. | ||||||
| #' Default is `rlang::current_env()` | ||||||
| #' @inheritParams cli::cli_abort | ||||||
| #' @return Invisible NULL | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this is needed. Also it is internal |
||||||
| #' @keywords internal | ||||||
| #' | ||||||
| #' @examples | ||||||
| #' \dontrun{ | ||||||
| #' cards:::..warn_or_error("This will be a message", FALSE) | ||||||
| #' cards:::..warn_or_error("This will be an error", TRUE) | ||||||
|
Comment on lines
+112
to
+113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be message_or_error right? |
||||||
| #' } | ||||||
| .message_or_error <- function(msg, error = FALSE, call = get_cli_abort_call(), | ||||||
| envir = rlang::current_env()) { | ||||||
| if(error){ | ||||||
| cli::cli_abort( | ||||||
| msg, call = call, .envir = envir | ||||||
| ) | ||||||
| }else{ | ||||||
| cli::cli_inform(msg, call = call, .envir = envir) | ||||||
| } | ||||||
| return(invisible()) | ||||||
|
|
||||||
|
Comment on lines
+124
to
+125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe without this completely? |
||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.