-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_ae_aedthdtc_aesdth.R
executable file
·102 lines (87 loc) · 2.9 KB
/
check_ae_aedthdtc_aesdth.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#' @title Check AEs with AEDTHDTC value but AESDTH not "Y"
#'
#' @description This check looks for AE entries with an AEDTHDTC (death date)
#' value and AESDTH not equal to "Y"
#'
#' @param AE Adverse Event SDTM dataset with variables USUBJID, AEDTHDTC,
#' AESDTH, AEDECOD, AETERM, and AESTDTC
#' @param preproc An optional company specific preprocessing script
#' @param ... Other arguments passed to methods
#'
#' @return Boolean value for whether the check passed or failed, with 'msg'
#' attribute if the check failed
#'
#' @export
#'
#' @importFrom dplyr %>% filter select
#' @importFrom tidyselect any_of
#'
#' @author Shumei Chi
#'
#' @examples
#'
#'
#' AE <- data.frame(
#' USUBJID = c(1:7),
#' AEDECOD = c(letters[1:5], "", NA),
#' AETERM = letters[1:7],
#' AESDTH = "Y",
#' AEDTHDTC = "2020-01-02",
#' AESTDTC = c(1:7),
#' AESPID = "FORMNAME-R:5/L:5XXXX",
#' stringsAsFactors=FALSE)
#'
#' # expect pass
#' check_ae_aedthdtc_aesdth(AE)
#' check_ae_aedthdtc_aesdth(AE,preproc=roche_derive_rave_row)
#'
#' # expect fail
#' AE1 <- AE
#' AE1$AESDTH[3] <- "N"
#' check_ae_aedthdtc_aesdth(AE1)
#' check_ae_aedthdtc_aesdth(AE1,preproc=roche_derive_rave_row)
#'
#' # expect fail with AESDTH = NA
#' AE2 <- AE
#' AE2$AESDTH[4] <- NA
#' check_ae_aedthdtc_aesdth(AE2)
#' check_ae_aedthdtc_aesdth(AE2,preproc=roche_derive_rave_row)
#'
#' # non-required variable missing
#' AE2$AESPID <- NULL
#' check_ae_aedthdtc_aesdth(AE2)
#' check_ae_aedthdtc_aesdth(AE2,preproc=roche_derive_rave_row)
#'
#' # required variable missing
#' AE2$AESDTH <- NULL
#' check_ae_aedthdtc_aesdth(AE2)
#' check_ae_aedthdtc_aesdth(AE2,preproc=roche_derive_rave_row)
#'
#'
#'
check_ae_aedthdtc_aesdth <- function(AE,preproc=identity,...) {
# Checks if required variables are present
if (AE %lacks_any% c("USUBJID", "AEDTHDTC", "AETERM", "AESDTH", "AEDECOD", "AESTDTC")) {
fail(lacks_msg(AE, c("USUBJID", "AEDTHDTC", "AETERM", "AESDTH", "AEDECOD", "AESTDTC")))
} else {
#Apply company specific preprocessing function
AE = preproc(AE,...)
# Rows where AEDTHDTC is not NA
has_aedthdtc <- !(is_sas_na(AE[["AEDTHDTC"]]))
# Rows where AESDTH != "Y", with expanded logic for NA values
no_aesdth <- !(AE[["AESDTH"]]=="Y") | (is_sas_na(AE[["AESDTH"]]))
# Subsets AE to select variables and rows where
# AESDTH != "Y" and AEDTHDTC has a value
df <- AE %>%
select(any_of(c("USUBJID", "AETERM", "AEDECOD", "AESTDTC","AEDTHDTC","AESDTH","RAVE"))) %>%
filter(has_aedthdtc, no_aesdth)
rownames(df) = NULL
# Outputs a resulting message depending on whether there are instances
# where AESDTH != "Y" and AEDTHDTC having a value
if (nrow(df)==0) {
pass()
} else {
fail(paste0("AE has ", nrow(df), " record(s) with AESDTH not equal to 'Y' where AEDTHDTC has a value. "), df)
}
}
}