Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanThoma committed Sep 20, 2024
1 parent c19e4ee commit 265ecea
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 173 deletions.
23 changes: 13 additions & 10 deletions R/derive_vars_cat.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' ~VSTEST, ~condition, ~AVALCAT1, ~AVALCA1N,
#' "Height", AVAL > 170, ">170 cm", 1,
#' "Height", AVAL <= 170, "<=170 cm", 2,
#' "Height", AVAL <= 160, "<=160 cm", 3,
#' "Height", AVAL <= 160, "<=160 cm", 3
#' )
#'
#' then `AVALCAT1` will be `"<=170 cm"`, as this is the first match for `AVAL`.
Expand All @@ -48,7 +48,7 @@
#' ~VSTEST, ~condition, ~AVALCAT1, ~AVALCA1N,
#' "Height", AVAL <= 160, "<=160 cm", 3,
#' "Height", AVAL <= 170, "<=170 cm", 2,
#' "Height", AVAL > 170, ">170 cm", 1,
#' "Height", AVAL > 170, ">170 cm", 1
#' )
#'
#' Then `AVAL <= 160` will lead to `AVALCAT1 == "<=160 cm"`,
Expand Down Expand Up @@ -92,8 +92,8 @@
#'
#' definition <- exprs(
#' ~condition, ~AVALCAT1, ~AVALCA1N, ~NEWCOL,
#' VSTEST == "Height" & AVAL > 140, ">140 cm", 1, "extra1",
#' VSTEST == "Height" & AVAL <= 140, "<=140 cm", 2, "extra2"
#' VSTEST == "Height" & AVAL > 160, ">160 cm", 1, "extra1",
#' VSTEST == "Height" & AVAL <= 160, "<=160 cm", 2, "extra2"
#' )
#' derive_vars_cat(
#' dataset = advs %>% dplyr::filter(VSTEST == "Height"),
Expand All @@ -102,8 +102,8 @@
#' # using by_vars:
#' definition2 <- exprs(
#' ~VSTEST, ~condition, ~AVALCAT1, ~AVALCA1N,
#' "Height", AVAL > 140, ">140 cm", 1,
#' "Height", AVAL <= 140, "<=140 cm", 2,
#' "Height", AVAL > 160, ">160 cm", 1,
#' "Height", AVAL <= 160, "<=160 cm", 2,
#' "Weight", AVAL > 70, ">70 kg", 1,
#' "Weight", AVAL <= 70, "<=70 kg", 2
#' )
Expand All @@ -119,7 +119,7 @@
#' ~VSTEST, ~condition, ~AVALCAT1, ~AVALCA1N,
#' "Height", AVAL > 170, ">170 cm", 1,
#' "Height", AVAL <= 170 & AVAL > 160, "<=170 cm", 2,
#' "Height", AVAL <= 160, "<=160 cm", 3,
#' "Height", AVAL <= 160, "<=160 cm", 3
#' )
#'
#' derive_vars_cat(
Expand All @@ -143,7 +143,12 @@ derive_vars_cat <- function(dataset,

# transform definition to tibble
names(definition) <- NULL
definition <- tibble::tribble(!!!definition)
definition <- tryCatch(
{tibble::tribble(!!!definition)},
error = function(e) {
# Catch the error and append your own message
stop("Failed to convert `definition` to tribble: ", e$message)
})
assert_data_frame(definition, required_vars = c(exprs(condition), by_vars))
if(!is.null(by_vars)){
# add condition
Expand Down Expand Up @@ -174,8 +179,6 @@ derive_vars_cat <- function(dataset,
}

## helper

definition
extend_condition <- function(cond, var, is) {
paste(cond, " & ", var, " == '", is, "'", sep = "")
}
239 changes: 141 additions & 98 deletions tests/testthat/_snaps/derive_vars_cat.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,123 @@
Code
result
Output
# A tibble: 10 x 5
# A tibble: 20 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <160 2
2 01-701-1023 Height 163. >=160 1
3 01-701-1028 Height 178. >=160 1
4 01-701-1033 Height 175. >=160 1
5 01-701-1034 Height 155. <160 2
6 01-701-1047 Height 149. <160 2
7 01-701-1097 Height 169. >=160 1
8 01-701-1111 Height 158. <160 2
9 01-701-1115 Height 182. >=160 1
10 01-701-1118 Height 180. >=160 1

# derive_vars_cat Test 2: Error when dataset is not a dataframe
1 01-701-1015 Height 147. <160 2
2 01-701-1023 Height 163. >=160 1
3 01-701-1028 Height 178. >=160 1
4 01-701-1033 Height 175. >=160 1
5 01-701-1034 Height NA <NA> NA
6 01-701-1047 Height NA <NA> NA
7 01-701-1097 Height 169. >=160 1
8 01-701-1111 Height 158. <160 2
9 01-701-1115 Height 182. >=160 1
10 01-701-1118 Height 180. >=160 1
11 01-701-1015 Weight 54.0 <NA> NA
12 01-701-1023 Weight 78.5 <NA> NA
13 01-701-1028 Weight 98.9 <NA> NA
14 01-701-1033 Weight 88.4 <NA> NA
15 01-701-1034 Weight NA <NA> NA
16 01-701-1047 Weight NA <NA> NA
17 01-701-1097 Weight 78.0 <NA> NA
18 01-701-1111 Weight 60.3 <NA> NA
19 01-701-1115 Weight 78.7 <NA> NA
20 01-701-1118 Weight 71.7 <NA> NA

---

Argument `dataset` must be class <data.frame>, but is a list.
Code
result2
Output
# A tibble: 20 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <160 2
2 01-701-1023 Height 163. >=160 1
3 01-701-1028 Height 178. >=160 1
4 01-701-1033 Height 175. >=160 1
5 01-701-1034 Height NA <NA> NA
6 01-701-1047 Height NA <NA> NA
7 01-701-1097 Height 169. >=160 1
8 01-701-1111 Height 158. <160 2
9 01-701-1115 Height 182. >=160 1
10 01-701-1118 Height 180. >=160 1
11 01-701-1015 Weight 54.0 <NA> NA
12 01-701-1023 Weight 78.5 <NA> NA
13 01-701-1028 Weight 98.9 <NA> NA
14 01-701-1033 Weight 88.4 <NA> NA
15 01-701-1034 Weight NA <NA> NA
16 01-701-1047 Weight NA <NA> NA
17 01-701-1097 Weight 78.0 <NA> NA
18 01-701-1111 Weight 60.3 <NA> NA
19 01-701-1115 Weight 78.7 <NA> NA
20 01-701-1118 Weight 71.7 <NA> NA

# derive_vars_cat Test 3: Error when definition is not an exprs object

Must specify at least one column using the `~name` syntax.

# derive_vars_cat Test 4: Error when required columns are missing from dataset

Required variable `VSTEST` is missing in `dataset`
Argument `definition` must be a list of expressions but is a tibble.
i To create a list of expressions use `exprs()`.

# derive_vars_cat Test 5: Correct behavior when no conditions are met

Code
result
Output
# A tibble: 10 x 5
# A tibble: 20 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <NA> NA
2 01-701-1023 Height 163. <NA> NA
3 01-701-1028 Height 178. <NA> NA
4 01-701-1033 Height 175. <NA> NA
5 01-701-1034 Height 155. <NA> NA
6 01-701-1047 Height 149. <NA> NA
7 01-701-1097 Height 169. <NA> NA
8 01-701-1111 Height 158. <NA> NA
9 01-701-1115 Height 182. <NA> NA
10 01-701-1118 Height 180. <NA> NA
1 01-701-1015 Height 147. <NA> NA
2 01-701-1023 Height 163. <NA> NA
3 01-701-1028 Height 178. <NA> NA
4 01-701-1033 Height 175. <NA> NA
5 01-701-1034 Height NA <NA> NA
6 01-701-1047 Height NA <NA> NA
7 01-701-1097 Height 169. <NA> NA
8 01-701-1111 Height 158. <NA> NA
9 01-701-1115 Height 182. <NA> NA
10 01-701-1118 Height 180. <NA> NA
11 01-701-1015 Weight 54.0 <NA> NA
12 01-701-1023 Weight 78.5 <NA> NA
13 01-701-1028 Weight 98.9 <NA> NA
14 01-701-1033 Weight 88.4 <NA> NA
15 01-701-1034 Weight NA <NA> NA
16 01-701-1047 Weight NA <NA> NA
17 01-701-1097 Weight 78.0 <NA> NA
18 01-701-1111 Weight 60.3 <NA> NA
19 01-701-1115 Weight 78.7 <NA> NA
20 01-701-1118 Weight 71.7 <NA> NA

# derive_vars_cat Test 6: Overlapping conditions handled correctly

Code
result
Output
# A tibble: 10 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height 147. <NA> NA
2 01-701-1023 Height 163. >=160 1
3 01-701-1028 Height 178. >=160 1
4 01-701-1033 Height 175. >=160 1
5 01-701-1034 Height 155. <NA> NA
6 01-701-1047 Height 149. <NA> NA
7 01-701-1097 Height 169. >=160 1
8 01-701-1111 Height 158. >155 2
9 01-701-1115 Height 182. >=160 1
10 01-701-1118 Height 180. >=160 1

# derive_vars_cat Test 7: Handles missing values in dataset correctly

Code
result
Output
# A tibble: 10 x 5
# A tibble: 20 x 5
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N
<chr> <chr> <dbl> <chr> <dbl>
1 01-701-1015 Height NA <NA> NA
2 01-701-1023 Height NA <NA> NA
3 01-701-1028 Height NA <NA> NA
4 01-701-1033 Height NA <NA> NA
5 01-701-1034 Height NA <NA> NA
6 01-701-1047 Height 149. <160 2
7 01-701-1097 Height 169. >=160 1
8 01-701-1111 Height 158. <160 2
9 01-701-1115 Height 182. >=160 1
10 01-701-1118 Height 180. >=160 1

# derive_vars_cat Test 8: Error when condition is missing from `definition`

Required variable `condition` is missing in `definition`

# derive_vars_cat Test 9: Conditions for multiple VSTESTs (Height and BILI)
1 01-701-1015 Height 147. <160 3
2 01-701-1023 Height 163. <170 2
3 01-701-1028 Height 178. >=170 1
4 01-701-1033 Height 175. >=170 1
5 01-701-1034 Height NA <NA> NA
6 01-701-1047 Height NA <NA> NA
7 01-701-1097 Height 169. <170 2
8 01-701-1111 Height 158. <160 3
9 01-701-1115 Height 182. >=170 1
10 01-701-1118 Height 180. >=170 1
11 01-701-1015 Weight 54.0 <NA> NA
12 01-701-1023 Weight 78.5 <NA> NA
13 01-701-1028 Weight 98.9 <NA> NA
14 01-701-1033 Weight 88.4 <NA> NA
15 01-701-1034 Weight NA <NA> NA
16 01-701-1047 Weight NA <NA> NA
17 01-701-1097 Weight 78.0 <NA> NA
18 01-701-1111 Weight 60.3 <NA> NA
19 01-701-1115 Weight 78.7 <NA> NA
20 01-701-1118 Weight 71.7 <NA> NA

# derive_vars_cat Test 8: Conditions for multiple VSTESTs (Height and Weight)

Code
result
Expand All @@ -99,42 +128,56 @@
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
2 01-701-1023 Height 163. Height >= 160 1
3 01-701-1028 Height 178. Height >= 160 1
4 01-701-1033 Height 175. Height >= 160 1
5 01-701-1034 Height NA <NA> NA
6 01-701-1047 Height NA <NA> NA
7 01-701-1097 Height 169. Height >= 160 1
8 01-701-1111 Height 158. Height < 160 2
9 01-701-1115 Height 182. Height >= 160 1
10 01-701-1118 Height 180. Height >= 160 1
11 01-701-1015 Weight 54.0 Weight < 66.68 2
12 01-701-1023 Weight 78.5 Weight >= 66.68 1
13 01-701-1028 Weight 98.9 Weight >= 66.68 1
14 01-701-1033 Weight 88.4 Weight >= 66.68 1
15 01-701-1034 Weight NA <NA> NA
16 01-701-1047 Weight NA <NA> NA
17 01-701-1097 Weight 78.0 Weight >= 66.68 1
18 01-701-1111 Weight 60.3 Weight < 66.68 2
19 01-701-1115 Weight 78.7 Weight >= 66.68 1
20 01-701-1118 Weight 71.7 Weight >= 66.68 1

# derive_vars_cat Test 9: Adding an extra variable (flag) to the dataset

Code
result
Output
# A tibble: 10 x 6
# A tibble: 20 x 6
USUBJID VSTEST AVAL AVALCAT1 AVALCA1N extra_var
<chr> <chr> <dbl> <chr> <dbl> <lgl>
1 01-701-1015 Height 147. <160 2 FALSE
2 01-701-1023 Height 163. >=160 1 TRUE
3 01-701-1028 Height 178. >=160 1 TRUE
4 01-701-1033 Height 175. >=160 1 TRUE
5 01-701-1034 Height 155. <160 2 FALSE
6 01-701-1047 Height 149. <160 2 FALSE
7 01-701-1097 Height 169. >=160 1 TRUE
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
1 01-701-1015 Height 147. <160 2 FALSE
2 01-701-1023 Height 163. >=160 1 TRUE
3 01-701-1028 Height 178. >=160 1 TRUE
4 01-701-1033 Height 175. >=160 1 TRUE
5 01-701-1034 Height NA <NA> NA NA
6 01-701-1047 Height NA <NA> NA NA
7 01-701-1097 Height 169. >=160 1 TRUE
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
11 01-701-1015 Weight 54.0 <NA> NA NA
12 01-701-1023 Weight 78.5 <NA> NA NA
13 01-701-1028 Weight 98.9 <NA> NA NA
14 01-701-1033 Weight 88.4 <NA> NA NA
15 01-701-1034 Weight NA <NA> NA NA
16 01-701-1047 Weight NA <NA> NA NA
17 01-701-1097 Weight 78.0 <NA> NA NA
18 01-701-1111 Weight 60.3 <NA> NA NA
19 01-701-1115 Weight 78.7 <NA> NA NA
20 01-701-1118 Weight 71.7 <NA> NA NA

# derive_vars_cat Test 11: definition has wrong shape

Failed to convert `definition` to tribble: Data must be rectangular.

Loading

0 comments on commit 265ecea

Please sign in to comment.