Skip to content

Commit

Permalink
styler
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanThoma committed Sep 5, 2024
1 parent dd38c99 commit 8b58650
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
39 changes: 31 additions & 8 deletions R/derive_vars_cat.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
54 changes: 29 additions & 25 deletions tests/testthat/_snaps/derive_vars_cat.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
result
Output
# A tibble: 254 x 5
# A tibble: 10 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <160 2
Expand All @@ -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

Expand All @@ -35,7 +34,7 @@
Code
result
Output
# A tibble: 254 x 5
# A tibble: 10 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <NA> NA
Expand All @@ -48,14 +47,13 @@
8 01-701-1111 Height 158. <NA> NA
9 01-701-1115 Height 182. <NA> NA
10 01-701-1118 Height 180. <NA> 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
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <NA> NA
Expand All @@ -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
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height NA <NA> NA
Expand All @@ -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`

Expand All @@ -99,27 +95,36 @@
Code
result
Output
# A tibble: 2,493 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
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
<chr> <chr> <dbl> <chr> <dbl>
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
<chr> <chr> <dbl> <chr> <dbl> <lgl>
1 01-701-1015 Height 147. <160 2 FALSE
Expand All @@ -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

24 changes: 23 additions & 1 deletion tests/testthat/test-derive_vars_cat.R
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down

0 comments on commit 8b58650

Please sign in to comment.