From 1e168c08c0a701fa253be3369027d907d6103e3e Mon Sep 17 00:00:00 2001 From: jtalboys Date: Mon, 30 Sep 2024 22:58:21 +0200 Subject: [PATCH 1/6] add fmt_fn, warning, and error cols to ard_attributes() output --- R/ard_attributes.R | 5 +++++ tests/testthat/_snaps/ard_attributes.md | 5 +++++ tests/testthat/test-ard_attributes.R | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/R/ard_attributes.R b/R/ard_attributes.R index cb2dd808c..1d69221a0 100644 --- a/R/ard_attributes.R +++ b/R/ard_attributes.R @@ -82,6 +82,11 @@ ard_attributes.data.frame <- function(data, ), context = "attributes" ) |> + dplyr::mutate( + fmt_fn = list(as.character), + warning = list(NULL), + error = list(NULL) + ) |> cards::tidy_ard_column_order() |> as_card() } diff --git a/tests/testthat/_snaps/ard_attributes.md b/tests/testthat/_snaps/ard_attributes.md index 5f8a70512..b7320fbfc 100644 --- a/tests/testthat/_snaps/ard_attributes.md +++ b/tests/testthat/_snaps/ard_attributes.md @@ -10,4 +10,9 @@ 2 var1 attributes class Variable Class character 3 var2 attributes label Variable Label UPPERCASE LETTERS 4 var2 attributes class Variable Class character + fmt_fn warning error + 1 .Primitive("as.character") NULL NULL + 2 .Primitive("as.character") NULL NULL + 3 .Primitive("as.character") NULL NULL + 4 .Primitive("as.character") NULL NULL diff --git a/tests/testthat/test-ard_attributes.R b/tests/testthat/test-ard_attributes.R index fe9ef0b14..e8245ed30 100644 --- a/tests/testthat/test-ard_attributes.R +++ b/tests/testthat/test-ard_attributes.R @@ -14,3 +14,10 @@ test_that("ard_attributes() errors when there is no dataframe", { "There is no method for objects of class ." ) }) + +test_that("ard_attributes() follows ard structure", { + expect_silent( + ard_attributes(ADSL[c("AGE", "AGEGR1")]) |> + check_ard_structure(method = FALSE) + ) +}) From fbe6e78baa7a7f6fd33ebcf43025481fee0ba2fa Mon Sep 17 00:00:00 2001 From: jtalboys Date: Mon, 30 Sep 2024 22:58:43 +0200 Subject: [PATCH 2/6] style --- R/ard_attributes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ard_attributes.R b/R/ard_attributes.R index 1d69221a0..ddff67a8c 100644 --- a/R/ard_attributes.R +++ b/R/ard_attributes.R @@ -86,7 +86,7 @@ ard_attributes.data.frame <- function(data, fmt_fn = list(as.character), warning = list(NULL), error = list(NULL) - ) |> + ) |> cards::tidy_ard_column_order() |> as_card() } From 79040ada76ed73a08a0d18cb463d672a432794c2 Mon Sep 17 00:00:00 2001 From: jtalboys Date: Tue, 1 Oct 2024 10:11:17 +0200 Subject: [PATCH 3/6] add label check in ard_attributes() --- R/ard_attributes.R | 19 +++++++++++++++---- tests/testthat/_snaps/ard_attributes.md | 12 ++++++++++-- tests/testthat/test-ard_attributes.R | 9 +++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/R/ard_attributes.R b/R/ard_attributes.R index ddff67a8c..622fb293f 100644 --- a/R/ard_attributes.R +++ b/R/ard_attributes.R @@ -57,6 +57,19 @@ ard_attributes.data.frame <- function(data, return(dplyr::tibble() |> as_card()) } + + # check label is a named list ------------------------------------------------ + if (!is_empty(label)) { + if (!is.list(label) || !is_named(label) || some(label, \(x) !is_string(x))) { + cli::cli_abort( + "The {.arg label} argument must be a named list with each element a string.", + call = get_cli_abort_call() + ) + } + } + + + variables |> lapply( FUN = function(y) { @@ -80,10 +93,8 @@ ard_attributes.data.frame <- function(data, .data$stat_name %in% "class" ~ "Variable Class", TRUE ~ .data$stat_name ), - context = "attributes" - ) |> - dplyr::mutate( - fmt_fn = list(as.character), + context = "attributes", + fmt_fn = ifelse(.data$stat_name %in% "label", list(as.character), list(NULL)), warning = list(NULL), error = list(NULL) ) |> diff --git a/tests/testthat/_snaps/ard_attributes.md b/tests/testthat/_snaps/ard_attributes.md index b7320fbfc..840faceba 100644 --- a/tests/testthat/_snaps/ard_attributes.md +++ b/tests/testthat/_snaps/ard_attributes.md @@ -12,7 +12,15 @@ 4 var2 attributes class Variable Class character fmt_fn warning error 1 .Primitive("as.character") NULL NULL - 2 .Primitive("as.character") NULL NULL + 2 NULL NULL NULL 3 .Primitive("as.character") NULL NULL - 4 .Primitive("as.character") NULL NULL + 4 NULL NULL NULL + +# ard_attributes() requires label as a named list + + Code + ard_attributes(ADSL[c("AGE", "AGEGR1")], label = list("test")) + Condition + Error in `ard_attributes()`: + ! The `label` argument must be a named list with each element a string. diff --git a/tests/testthat/test-ard_attributes.R b/tests/testthat/test-ard_attributes.R index e8245ed30..427a03f88 100644 --- a/tests/testthat/test-ard_attributes.R +++ b/tests/testthat/test-ard_attributes.R @@ -21,3 +21,12 @@ test_that("ard_attributes() follows ard structure", { check_ard_structure(method = FALSE) ) }) + +test_that("ard_attributes() requires label as a named list", { + expect_snapshot( + error = TRUE, + ard_attributes(ADSL[c("AGE", "AGEGR1")], + label = list("test") + ) + ) +}) From 359cbb3cb28de2b5c808ea66c1f82b131d56f2e0 Mon Sep 17 00:00:00 2001 From: jtalboys Date: Tue, 1 Oct 2024 10:23:36 +0200 Subject: [PATCH 4/6] update tests for those using ard_attributes() --- tests/testthat/_snaps/mock.md | 14 ++++++++------ tests/testthat/_snaps/print.md | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/testthat/_snaps/mock.md b/tests/testthat/_snaps/mock.md index ffe93c0bc..4e64ed691 100644 --- a/tests/testthat/_snaps/mock.md +++ b/tests/testthat/_snaps/mock.md @@ -131,13 +131,15 @@ Code mock_attributes(label = list(AGE = "Age", BMIBL = "Baseline BMI")) Message - {cards} data frame: 4 x 5 + {cards} data frame: 4 x 8 Output - variable context stat_name stat_label stat - 1 AGE attribut… label Variable… Age - 2 AGE attribut… class Variable… logical - 3 BMIBL attribut… label Variable… Baseline… - 4 BMIBL attribut… class Variable… logical + variable context stat_name stat_label stat fmt_fn + 1 AGE attribut… label Variable… Age + 2 AGE attribut… class Variable… logical NULL + 3 BMIBL attribut… label Variable… Baseline… + 4 BMIBL attribut… class Variable… logical NULL + Message + i 2 more variables: warning, error # mock_attributes() messaging diff --git a/tests/testthat/_snaps/print.md b/tests/testthat/_snaps/print.md index de1781e0e..b9b5beb23 100644 --- a/tests/testthat/_snaps/print.md +++ b/tests/testthat/_snaps/print.md @@ -90,7 +90,7 @@ {cards} data frame: 4 x 8 Output variable context stat_name stat_label stat fmt_fn - 1 mpg attribut… label Variable… mpg NULL + 1 mpg attribut… label Variable… mpg 2 mpg attribut… class Variable… numeric NULL 3 mpg continuo… mean Mean 20.091 1 4 mpg continuo… vcov vcov 1.265, -1.265, -1.265, 3.113 1 From 3ccb93844be8392cdd7cdf2b926c5849184dc845 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Tue, 1 Oct 2024 20:23:49 -0700 Subject: [PATCH 5/6] increased width to snapshot test so data frame isn't split into two pieces --- tests/testthat/_snaps/ard_attributes.md | 15 +++++---------- tests/testthat/test-ard_attributes.R | 3 +++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/testthat/_snaps/ard_attributes.md b/tests/testthat/_snaps/ard_attributes.md index 840faceba..e690589a3 100644 --- a/tests/testthat/_snaps/ard_attributes.md +++ b/tests/testthat/_snaps/ard_attributes.md @@ -5,16 +5,11 @@ attr(df$var1, "label") <- "Lowercase Letters" as.data.frame(ard_attributes(df, variables = everything(), label = list(var2 = "UPPERCASE LETTERS"))) Output - variable context stat_name stat_label stat - 1 var1 attributes label Variable Label Lowercase Letters - 2 var1 attributes class Variable Class character - 3 var2 attributes label Variable Label UPPERCASE LETTERS - 4 var2 attributes class Variable Class character - fmt_fn warning error - 1 .Primitive("as.character") NULL NULL - 2 NULL NULL NULL - 3 .Primitive("as.character") NULL NULL - 4 NULL NULL NULL + variable context stat_name stat_label stat fmt_fn warning error + 1 var1 attributes label Variable Label Lowercase Letters .Primitive("as.character") NULL NULL + 2 var1 attributes class Variable Class character NULL NULL NULL + 3 var2 attributes label Variable Label UPPERCASE LETTERS .Primitive("as.character") NULL NULL + 4 var2 attributes class Variable Class character NULL NULL NULL # ard_attributes() requires label as a named list diff --git a/tests/testthat/test-ard_attributes.R b/tests/testthat/test-ard_attributes.R index 427a03f88..2cf2a3a92 100644 --- a/tests/testthat/test-ard_attributes.R +++ b/tests/testthat/test-ard_attributes.R @@ -1,4 +1,7 @@ +skip_if_not(is_pkg_installed("withr")) + test_that("ard_attributes() works", { + withr::local_options(list(width = 120)) expect_snapshot({ df <- dplyr::tibble(var1 = letters, var2 = LETTERS) attr(df$var1, "label") <- "Lowercase Letters" From ba50856f089681f3ae08198a8365d44bbe733cc5 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Tue, 1 Oct 2024 21:00:29 -0700 Subject: [PATCH 6/6] Update NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7dfb00842..698126360 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # cards 0.2.2.9015 +* Add columns `'fmt_fn'`, `'warning'`, and `'errors'` to `ard_attributes()` output. (#327) + * Add checks for factors with no levels, or any levels that are `NA` into `ard_*` functions (#255) * Any rows with `NA` or `NaN` values in the `.by` columns specified in `ard_stack()` are now removed from all calculations. (#320)