From 8b586504258b2bae189a5ecaca39885f967d5b84 Mon Sep 17 00:00:00 2001 From: Stefan Pascal Thoma Date: Thu, 5 Sep 2024 19:44:20 +0000 Subject: [PATCH] styler --- R/derive_vars_cat.R | 39 +++++++++++++---- tests/testthat/_snaps/derive_vars_cat.md | 54 +++++++++++++----------- tests/testthat/test-derive_vars_cat.R | 24 ++++++++++- 3 files changed, 83 insertions(+), 34 deletions(-) diff --git a/R/derive_vars_cat.R b/R/derive_vars_cat.R index e0c4abf21a..9b763bc0a0 100644 --- a/R/derive_vars_cat.R +++ b/R/derive_vars_cat.R @@ -10,18 +10,41 @@ #' #' @examples #' -#' advs <- pharmaverseadam::advs +#' advs <- tibble::tribble( +#' ~USUBJID, ~VSTEST, ~AVAL, +#' "01-701-1015", "Height", 147.32, +#' "01-701-1015", "Weight", 53.98, +#' "01-701-1023", "Height", 162.56, +#' "01-701-1023", "Weight", 78.47, +#' "01-701-1028", "Height", 177.8, +#' "01-701-1028", "Weight", 98.88, +#' "01-701-1033", "Height", 175.26, +#' "01-701-1033", "Weight", 88.45, +#' "01-701-1034", "Height", 154.94, +#' "01-701-1034", "Weight", 63.5, +#' "01-701-1047", "Height", 148.59, +#' "01-701-1047", "Weight", 66.23, +#' "01-701-1097", "Height", 168.91, +#' "01-701-1097", "Weight", 78.02, +#' "01-701-1111", "Height", 158.24, +#' "01-701-1111", "Weight", 60.33, +#' "01-701-1115", "Height", 181.61, +#' "01-701-1115", "Weight", 78.7, +#' "01-701-1118", "Height", 180.34, +#' "01-701-1118", "Weight", 71.67 +#' ) #' #' definition <- exprs( -#' ~condition, ~AVALCAT1, ~AVALCA1N, ~NEWCOL, -#' VSTESTCD == "Height" & AVAL > 140, ">140 cm", 1, "extra1", -#' VSTESTCD == "Height" & AVAL <= 140, "<=140 cm", 2, "extra2" +#' ~condition, ~AVALCAT1, ~AVALCA1N, ~NEWCOL, +#' VSTESTCD == "Height" & AVAL > 140, ">140 cm", 1, "extra1", +#' VSTESTCD == "Height" & AVAL <= 140, "<=140 cm", 2, "extra2" #' ) #' -#' derive_vars_cat(dataset = advs %>% filter(VSTESTCD == "Height"), -#' definition = definition) %>% +#' derive_vars_cat( +#' dataset = advs %>% filter(VSTESTCD == "Height"), +#' definition = definition +#' ) %>% #' select(USUBJID, VSTESTCD, AVAL, AVALCA1N, AVALCAT1) - derive_vars_cat <- function(dataset, definition) { # assertions @@ -36,7 +59,7 @@ derive_vars_cat <- function(dataset, # extract new variable names and conditions - new_col_names <- names(definition) [!names(definition) == "condition"] + new_col_names <- names(definition)[!names(definition) == "condition"] condition <- definition[["condition"]] # could also be outside of the function # (re)apply the function for each new variable name and iteratively derive the categories diff --git a/tests/testthat/_snaps/derive_vars_cat.md b/tests/testthat/_snaps/derive_vars_cat.md index f117a99afb..99a42708eb 100644 --- a/tests/testthat/_snaps/derive_vars_cat.md +++ b/tests/testthat/_snaps/derive_vars_cat.md @@ -3,7 +3,7 @@ Code result Output - # A tibble: 254 x 5 + # A tibble: 10 x 5 USUBJID VSTEST AVAL AVALCAT1 AVALCA1N 1 01-701-1015 Height 147. <160 2 @@ -16,7 +16,6 @@ 8 01-701-1111 Height 158. <160 2 9 01-701-1115 Height 182. >=160 1 10 01-701-1118 Height 180. >=160 1 - # i 244 more rows # derive_vars_cat Test 2: Error when dataset is not a dataframe @@ -35,7 +34,7 @@ Code result Output - # A tibble: 254 x 5 + # A tibble: 10 x 5 USUBJID VSTEST AVAL AVALCAT1 AVALCA1N 1 01-701-1015 Height 147. NA @@ -48,14 +47,13 @@ 8 01-701-1111 Height 158. NA 9 01-701-1115 Height 182. NA 10 01-701-1118 Height 180. NA - # i 244 more rows # derive_vars_cat Test 6: Overlapping conditions handled correctly Code result Output - # A tibble: 254 x 5 + # A tibble: 10 x 5 USUBJID VSTEST AVAL AVALCAT1 AVALCA1N 1 01-701-1015 Height 147. NA @@ -68,14 +66,13 @@ 8 01-701-1111 Height 158. >155 2 9 01-701-1115 Height 182. >=160 1 10 01-701-1118 Height 180. >=160 1 - # i 244 more rows # derive_vars_cat Test 7: Handles missing values in dataset correctly Code result Output - # A tibble: 254 x 5 + # A tibble: 10 x 5 USUBJID VSTEST AVAL AVALCAT1 AVALCA1N 1 01-701-1015 Height NA NA @@ -88,9 +85,8 @@ 8 01-701-1111 Height 158. <160 2 9 01-701-1115 Height 182. >=160 1 10 01-701-1118 Height 180. >=160 1 - # i 244 more rows -# derive_vars_cat Test 8: Error when condition is missing from exprs() definition object +# derive_vars_cat Test 8: Error when condition is missing from `definition` Required variable `condition` is missing in `definition` @@ -99,27 +95,36 @@ Code result Output - # A tibble: 2,493 x 5 - USUBJID VSTEST AVAL AVALCAT1 AVALCA1N - - 1 01-701-1015 Height 147. Height < 160 2 - 2 01-701-1015 Weight 54.0 Weight < 66.68 4 - 3 01-701-1015 Weight 54.4 Weight < 66.68 4 - 4 01-701-1015 Weight 53.1 Weight < 66.68 4 - 5 01-701-1015 Weight 54.0 Weight < 66.68 4 - 6 01-701-1015 Weight 53.1 Weight < 66.68 4 - 7 01-701-1015 Weight 53.1 Weight < 66.68 4 - 8 01-701-1015 Weight 53.1 Weight < 66.68 4 - 9 01-701-1015 Weight 53.1 Weight < 66.68 4 - 10 01-701-1015 Weight 53.1 Weight < 66.68 4 - # i 2,483 more rows + # A tibble: 20 x 5 + USUBJID VSTEST AVAL AVALCAT1 AVALCA1N + + 1 01-701-1015 Height 147. Height < 160 2 + 2 01-701-1015 Weight 54.0 Weight < 66.68 4 + 3 01-701-1023 Height 163. Height >= 160 1 + 4 01-701-1023 Weight 78.5 Weight >= 66.68 3 + 5 01-701-1028 Height 178. Height >= 160 1 + 6 01-701-1028 Weight 98.9 Weight >= 66.68 3 + 7 01-701-1033 Height 175. Height >= 160 1 + 8 01-701-1033 Weight 88.4 Weight >= 66.68 3 + 9 01-701-1034 Height 155. Height < 160 2 + 10 01-701-1034 Weight 63.5 Weight < 66.68 4 + 11 01-701-1047 Height 149. Height < 160 2 + 12 01-701-1047 Weight 66.2 Weight < 66.68 4 + 13 01-701-1097 Height 169. Height >= 160 1 + 14 01-701-1097 Weight 78.0 Weight >= 66.68 3 + 15 01-701-1111 Height 158. Height < 160 2 + 16 01-701-1111 Weight 60.3 Weight < 66.68 4 + 17 01-701-1115 Height 182. Height >= 160 1 + 18 01-701-1115 Weight 78.7 Weight >= 66.68 3 + 19 01-701-1118 Height 180. Height >= 160 1 + 20 01-701-1118 Weight 71.7 Weight >= 66.68 3 # derive_vars_cat Test 10: Adding an extra variable (flag) to the dataset Code result Output - # A tibble: 254 x 6 + # A tibble: 10 x 6 USUBJID VSTEST AVAL AVALCAT1 AVALCA1N extra_var 1 01-701-1015 Height 147. <160 2 FALSE @@ -132,5 +137,4 @@ 8 01-701-1111 Height 158. <160 2 FALSE 9 01-701-1115 Height 182. >=160 1 TRUE 10 01-701-1118 Height 180. >=160 1 TRUE - # i 244 more rows diff --git a/tests/testthat/test-derive_vars_cat.R b/tests/testthat/test-derive_vars_cat.R index 3876df15a9..c20ddacaf0 100644 --- a/tests/testthat/test-derive_vars_cat.R +++ b/tests/testthat/test-derive_vars_cat.R @@ -1,5 +1,27 @@ # Load the advs dataset -advs <- pharmaverseadam::advs +advs <- tibble::tribble( + ~USUBJID, ~VSTEST, ~AVAL, + "01-701-1015", "Height", 147.32, + "01-701-1015", "Weight", 53.98, + "01-701-1023", "Height", 162.56, + "01-701-1023", "Weight", 78.47, + "01-701-1028", "Height", 177.8, + "01-701-1028", "Weight", 98.88, + "01-701-1033", "Height", 175.26, + "01-701-1033", "Weight", 88.45, + "01-701-1034", "Height", 154.94, + "01-701-1034", "Weight", 63.5, + "01-701-1047", "Height", 148.59, + "01-701-1047", "Weight", 66.23, + "01-701-1097", "Height", 168.91, + "01-701-1097", "Weight", 78.02, + "01-701-1111", "Height", 158.24, + "01-701-1111", "Weight", 60.33, + "01-701-1115", "Height", 181.61, + "01-701-1115", "Weight", 78.7, + "01-701-1118", "Height", 180.34, + "01-701-1118", "Weight", 71.67 +) ## Test 1: Basic functionality with advs dataset ---- test_that("derive_vars_cat Test 1: Basic functionality with advs dataset", {