From 47e08c86997d6539744783cc32d747aa342b461e Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 6 Nov 2023 16:06:14 +0000 Subject: [PATCH 01/66] draft --- R/calculate_study_day.R | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 R/calculate_study_day.R diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R new file mode 100644 index 00000000..8db5ba51 --- /dev/null +++ b/R/calculate_study_day.R @@ -0,0 +1,27 @@ +#' `calculate_study_day` Performs the Study Day Calculation +#' +calculate_study_day <- function(ds_in, refdt, tgdt) { + assertthat::assert_that(is.data.frame(ds_in)) + assertthat::assert_that(hasName(ds_in, refdt)) + assertthat::assert_that(hasName(ds_in, tgdt)) + + # question: should I assume that refdt/tgdt was converted to Date already? + # If assume that refdt and tgdt are already dates + if (!("Date" %in% class(ds_in[[refdt]]) && "Date" %in% class(ds_in[[tgdt]]))) { + stop("Reference and target date has to be Date objects.") + } + refdt_vector <- ds_in[[refdt]] + tgdt_vector <- ds_in[[tgdt]] + + res <- ifelse( + test = refdt_vector <= tgdt_vector, + yes = refdt_vector - tgdt_vector + 1, + no = ifelse( + test = refdt_vector > tgdt_vector, + yes = tgdt_vector - refdt_vector, + no = NA + ) + ) + return(res) +} + From 05a14d2cb1d163b9c63af4c90027780da49c4a9a Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 6 Dec 2023 04:51:42 +0000 Subject: [PATCH 02/66] add reference df so that DM can be joined on the fly --- R/calculate_study_day.R | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 8db5ba51..8a6b9e87 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -1,9 +1,31 @@ #' `calculate_study_day` Performs the Study Day Calculation #' -calculate_study_day <- function(ds_in, refdt, tgdt) { +calculate_study_day <- function(ds_in, + ds_ref, + refdt, + tgdt, + merge_key = "USUBJID") { + assertthat::assert_that(is.data.frame(ds_in)) - assertthat::assert_that(hasName(ds_in, refdt)) + assertthat::assert_that(is.data.frame(ds_ref)) + assertthat::assert_that(hasName(ds_ref, refdt)) assertthat::assert_that(hasName(ds_in, tgdt)) + assertthat::assert_that(hasName(ds_ref, merge_key)) + assertthat::assert_that(hasName(ds_in, merge_key)) + + if (!identical(ds_in, ds_ref)) { + ds_ref <- unique(ds_ref[c(merge_key, refdt)]) + + check_refdt_uniqueness <- ds_ref %>% + dplyr::group_by(dplyr::pick({{merge_key}})) %>% + dplyr::filter(n() > 1) + if (nrow(check_refdt_uniqueness) > 0) { + stop("Reference date is not unique for each patient!") + } + + ds_in <- ds_in %>% + dplyr::left_join(ds_ref, by = structure(names = merge_key, .Data = merge_key)) + } # question: should I assume that refdt/tgdt was converted to Date already? # If assume that refdt and tgdt are already dates From ef235acaa5c838dd36a3786bac132f2f385792d6 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 6 Dec 2023 19:34:28 +0000 Subject: [PATCH 03/66] add roxygen doc --- R/calculate_study_day.R | 81 ++++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 8a6b9e87..3e6c4ba6 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -1,36 +1,90 @@ -#' `calculate_study_day` Performs the Study Day Calculation +#' `calculate_study_day` performs study day calculation +#' @description +#' This function takes the an input data.frame and a reference data.frame (which +#' is DM domain in most cases), and calculate the study day from reference date +#' and target date. In case of unexpected conditions like reference date is not +#' unique for each patient, or reference and input dates are not actual dates, +#' NA will be returned for those records. #' +#' @md +#' @param ds_in input data.frame that contains the target date. +#' @param ds_dm reference date.frame that contains the reference date. +#' @param refdt reference date from `ds_dm` that will be used as reference to +#' calculate the study day. +#' @param tgdt target date from `ds_in` that will be used to calcualte the study +#' day. +#' @param study_day_var the new study day variable name in the output. For +#' example, AESTDY for AE domain for CMSTDY for CM domain. +#' @param merge_key character to represents the merging key between `ds_in` and +#' `ds_dm`. +#' +#' @return a data.frame that takes all columns from `ds_in` and a new variable +#' to represent the calculated study day. +#' +#' @examples +#' \dontrun{ +#' ae <- data.frame( +#' USUBJID = c("study123-123", "study123-124", "study123-125"), +#' AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") +#' ) +#' dm <- data.frame( +#' USUBJID = c("study123-123", "study123-124", "study123-125"), +#' RFSTDTC = c("2012-02-01", "2012-04-14", NA) +#' ) +#' ae$AESTDTC <- as.Date(ae$AESTDTC) +#' dm$RFSTDTC <- as.Date(dm$RFSTDTC) +#' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") +#' } +#' +#' + calculate_study_day <- function(ds_in, - ds_ref, + ds_dm, refdt, tgdt, + study_day_var, merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(ds_in)) - assertthat::assert_that(is.data.frame(ds_ref)) - assertthat::assert_that(hasName(ds_ref, refdt)) + assertthat::assert_that(is.data.frame(ds_dm)) + assertthat::assert_that(hasName(ds_dm, refdt)) assertthat::assert_that(hasName(ds_in, tgdt)) - assertthat::assert_that(hasName(ds_ref, merge_key)) + assertthat::assert_that(hasName(ds_dm, merge_key)) assertthat::assert_that(hasName(ds_in, merge_key)) + assertthat::assert_that(is.character(study_day_var)) - if (!identical(ds_in, ds_ref)) { - ds_ref <- unique(ds_ref[c(merge_key, refdt)]) + if (!identical(ds_in, ds_dm)) { + ds_dm <- unique(ds_dm[c(merge_key, refdt)]) - check_refdt_uniqueness <- ds_ref %>% + check_refdt_uniqueness <- ds_dm %>% dplyr::group_by(dplyr::pick({{merge_key}})) %>% - dplyr::filter(n() > 1) + dplyr::filter(dplyr::n() > 1) if (nrow(check_refdt_uniqueness) > 0) { - stop("Reference date is not unique for each patient!") + warning( + "Reference date is not unique for each patient! ", + "Patient without unique reference date will be ingored. ", + "NA will be returned for such records." + ) + ds_dm <- ds_dm[ + !ds_dm[[merge_key]] %in% check_refdt_uniqueness[[merge_key]], + ] } ds_in <- ds_in %>% - dplyr::left_join(ds_ref, by = structure(names = merge_key, .Data = merge_key)) + dplyr::left_join( + ds_dm, by = structure(names = merge_key, .Data = merge_key) + ) } # question: should I assume that refdt/tgdt was converted to Date already? # If assume that refdt and tgdt are already dates if (!("Date" %in% class(ds_in[[refdt]]) && "Date" %in% class(ds_in[[tgdt]]))) { - stop("Reference and target date has to be Date objects.") + warning( + "Reference and target date has to be Date objects. ", + "If either is not, NA will be returned for study day." + ) + ds_in[study_day_var] <- NA + return(ds_in) } refdt_vector <- ds_in[[refdt]] tgdt_vector <- ds_in[[tgdt]] @@ -44,6 +98,7 @@ calculate_study_day <- function(ds_in, no = NA ) ) - return(res) + ds_in[study_day_var] <- res + return(ds_in) } From d3814cb8f262c039e3f150b4f90f41fe51056124 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 6 Dec 2023 20:06:12 +0000 Subject: [PATCH 04/66] doc update --- man/calculate_study_day.Rd | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 man/calculate_study_day.Rd diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd new file mode 100644 index 00000000..ec71af05 --- /dev/null +++ b/man/calculate_study_day.Rd @@ -0,0 +1,60 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calculate_study_day.R +\name{calculate_study_day} +\alias{calculate_study_day} +\title{\code{calculate_study_day} performs study day calculation} +\usage{ +calculate_study_day( + ds_in, + ds_dm, + refdt, + tgdt, + study_day_var, + merge_key = "USUBJID" +) +} +\arguments{ +\item{ds_in}{input data.frame that contains the target date.} + +\item{ds_dm}{reference date.frame that contains the reference date.} + +\item{refdt}{reference date from \code{ds_dm} that will be used as reference to +calculate the study day.} + +\item{tgdt}{target date from \code{ds_in} that will be used to calcualte the study +day.} + +\item{study_day_var}{the new study day variable name in the output. For +example, AESTDY for AE domain for CMSTDY for CM domain.} + +\item{merge_key}{character to represents the merging key between \code{ds_in} and +\code{ds_dm}.} +} +\value{ +a data.frame that takes all columns from \code{ds_in} and a new variable +to represent the calculated study day. +} +\description{ +This function takes the an input data.frame and a reference data.frame (which +is DM domain in most cases), and calculate the study day from reference date +and target date. In case of unexpected conditions like reference date is not +unique for each patient, or reference and input dates are not actual dates, +NA will be returned for those records. +} +\examples{ +\dontrun{ + ae <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") + ) + dm <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + RFSTDTC = c("2012-02-01", "2012-04-14", NA) + ) + ae$AESTDTC <- as.Date(ae$AESTDTC) + dm$RFSTDTC <- as.Date(dm$RFSTDTC) + calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") +} + + +} From 1333ca919099234514b26efe60fbaca0a370bb21 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Tue, 16 Jan 2024 21:17:31 +0000 Subject: [PATCH 05/66] rename ds_in as sdtm_in --- R/calculate_study_day.R | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 3e6c4ba6..fd5754db 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -7,18 +7,18 @@ #' NA will be returned for those records. #' #' @md -#' @param ds_in input data.frame that contains the target date. +#' @param sdtm_in input data.frame that contains the target date. #' @param ds_dm reference date.frame that contains the reference date. #' @param refdt reference date from `ds_dm` that will be used as reference to #' calculate the study day. -#' @param tgdt target date from `ds_in` that will be used to calcualte the study +#' @param tgdt target date from `sdtm_in` that will be used to calcualte the study #' day. #' @param study_day_var the new study day variable name in the output. For #' example, AESTDY for AE domain for CMSTDY for CM domain. -#' @param merge_key character to represents the merging key between `ds_in` and +#' @param merge_key character to represents the merging key between `sdtm_in` and #' `ds_dm`. #' -#' @return a data.frame that takes all columns from `ds_in` and a new variable +#' @return a data.frame that takes all columns from `sdtm_in` and a new variable #' to represent the calculated study day. #' #' @examples @@ -38,22 +38,22 @@ #' #' -calculate_study_day <- function(ds_in, +calculate_study_day <- function(sdtm_in, ds_dm, refdt, tgdt, study_day_var, merge_key = "USUBJID") { - assertthat::assert_that(is.data.frame(ds_in)) + assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(ds_dm)) assertthat::assert_that(hasName(ds_dm, refdt)) - assertthat::assert_that(hasName(ds_in, tgdt)) + assertthat::assert_that(hasName(sdtm_in, tgdt)) assertthat::assert_that(hasName(ds_dm, merge_key)) - assertthat::assert_that(hasName(ds_in, merge_key)) + assertthat::assert_that(hasName(sdtm_in, merge_key)) assertthat::assert_that(is.character(study_day_var)) - if (!identical(ds_in, ds_dm)) { + if (!identical(sdtm_in, ds_dm)) { ds_dm <- unique(ds_dm[c(merge_key, refdt)]) check_refdt_uniqueness <- ds_dm %>% @@ -70,7 +70,7 @@ calculate_study_day <- function(ds_in, ] } - ds_in <- ds_in %>% + sdtm_in <- sdtm_in %>% dplyr::left_join( ds_dm, by = structure(names = merge_key, .Data = merge_key) ) @@ -78,16 +78,16 @@ calculate_study_day <- function(ds_in, # question: should I assume that refdt/tgdt was converted to Date already? # If assume that refdt and tgdt are already dates - if (!("Date" %in% class(ds_in[[refdt]]) && "Date" %in% class(ds_in[[tgdt]]))) { + if (!("Date" %in% class(sdtm_in[[refdt]]) && "Date" %in% class(sdtm_in[[tgdt]]))) { warning( "Reference and target date has to be Date objects. ", "If either is not, NA will be returned for study day." ) - ds_in[study_day_var] <- NA - return(ds_in) + sdtm_in[study_day_var] <- NA + return(sdtm_in) } - refdt_vector <- ds_in[[refdt]] - tgdt_vector <- ds_in[[tgdt]] + refdt_vector <- sdtm_in[[refdt]] + tgdt_vector <- sdtm_in[[tgdt]] res <- ifelse( test = refdt_vector <= tgdt_vector, @@ -98,7 +98,7 @@ calculate_study_day <- function(ds_in, no = NA ) ) - ds_in[study_day_var] <- res - return(ds_in) + sdtm_in[study_day_var] <- res + return(sdtm_in) } From 10c22337db89698e272714a59afe25890b73ccfe Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 17 Jan 2024 03:06:00 +0000 Subject: [PATCH 06/66] replace ds_dm with dm_domain --- R/calculate_study_day.R | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index fd5754db..a61df9dd 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -8,15 +8,15 @@ #' #' @md #' @param sdtm_in input data.frame that contains the target date. -#' @param ds_dm reference date.frame that contains the reference date. -#' @param refdt reference date from `ds_dm` that will be used as reference to +#' @param dm_domain reference date.frame that contains the reference date. +#' @param refdt reference date from `dm_domain` that will be used as reference to #' calculate the study day. #' @param tgdt target date from `sdtm_in` that will be used to calcualte the study #' day. #' @param study_day_var the new study day variable name in the output. For #' example, AESTDY for AE domain for CMSTDY for CM domain. #' @param merge_key character to represents the merging key between `sdtm_in` and -#' `ds_dm`. +#' `dm_domain`. #' #' @return a data.frame that takes all columns from `sdtm_in` and a new variable #' to represent the calculated study day. @@ -39,24 +39,24 @@ #' calculate_study_day <- function(sdtm_in, - ds_dm, + dm_domain = DM, refdt, tgdt, study_day_var, merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(sdtm_in)) - assertthat::assert_that(is.data.frame(ds_dm)) - assertthat::assert_that(hasName(ds_dm, refdt)) + assertthat::assert_that(is.data.frame(dm_domain)) + assertthat::assert_that(hasName(dm_domain, refdt)) assertthat::assert_that(hasName(sdtm_in, tgdt)) - assertthat::assert_that(hasName(ds_dm, merge_key)) + assertthat::assert_that(hasName(dm_domain, merge_key)) assertthat::assert_that(hasName(sdtm_in, merge_key)) assertthat::assert_that(is.character(study_day_var)) - if (!identical(sdtm_in, ds_dm)) { - ds_dm <- unique(ds_dm[c(merge_key, refdt)]) + if (!identical(sdtm_in, dm_domain)) { + dm_domain <- unique(dm_domain[c(merge_key, refdt)]) - check_refdt_uniqueness <- ds_dm %>% + check_refdt_uniqueness <- dm_domain %>% dplyr::group_by(dplyr::pick({{merge_key}})) %>% dplyr::filter(dplyr::n() > 1) if (nrow(check_refdt_uniqueness) > 0) { @@ -65,14 +65,14 @@ calculate_study_day <- function(sdtm_in, "Patient without unique reference date will be ingored. ", "NA will be returned for such records." ) - ds_dm <- ds_dm[ - !ds_dm[[merge_key]] %in% check_refdt_uniqueness[[merge_key]], + dm_domain <- dm_domain[ + !dm_domain[[merge_key]] %in% check_refdt_uniqueness[[merge_key]], ] } sdtm_in <- sdtm_in %>% dplyr::left_join( - ds_dm, by = structure(names = merge_key, .Data = merge_key) + dm_domain, by = structure(names = merge_key, .Data = merge_key) ) } From 2881071c83691aafc8f2c90413ee7ce4823c9289 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 17 Jan 2024 03:10:00 +0000 Subject: [PATCH 07/66] add default value for refdt --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index a61df9dd..d37c324c 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -40,7 +40,7 @@ calculate_study_day <- function(sdtm_in, dm_domain = DM, - refdt, + refdt = "RFSTDTC", tgdt, study_day_var, merge_key = "USUBJID") { From 4eb2926d9194e2da60ddc0c35fba1d4d719ef77e Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 17 Jan 2024 04:31:49 +0000 Subject: [PATCH 08/66] add matching check --- R/calculate_study_day.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index d37c324c..80158957 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -52,6 +52,13 @@ calculate_study_day <- function(sdtm_in, assertthat::assert_that(hasName(dm_domain, merge_key)) assertthat::assert_that(hasName(sdtm_in, merge_key)) assertthat::assert_that(is.character(study_day_var)) + # check tgdt and study_day_var matching, for example, CMSTDTC matches CMSTDY + if (gsub("DTC", "", tgdt) != gsub("DY", "", study_day_var)) { + warning( + "Target date and the returned study day doesn't match. ", + "Expecting matching date and study day, for example, CMENDTC and CMENDY" + ) + } if (!identical(sdtm_in, dm_domain)) { dm_domain <- unique(dm_domain[c(merge_key, refdt)]) From 1caeb91e4d9e7f78f4a3ffd0f4d287f14794ef66 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 21 Jan 2024 22:41:46 +0000 Subject: [PATCH 09/66] refdt and tgdt have to be ISO format, otherwise NA and warning --- R/calculate_study_day.R | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 80158957..bcf61994 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -83,16 +83,24 @@ calculate_study_day <- function(sdtm_in, ) } - # question: should I assume that refdt/tgdt was converted to Date already? - # If assume that refdt and tgdt are already dates - if (!("Date" %in% class(sdtm_in[[refdt]]) && "Date" %in% class(sdtm_in[[tgdt]]))) { - warning( - "Reference and target date has to be Date objects. ", - "If either is not, NA will be returned for study day." - ) - sdtm_in[study_day_var] <- NA - return(sdtm_in) - } + # refdt/tgdt should be in ISO format, otherwise throw warning + tryCatch( + sdtm_in[[refdt]] <- as.Date(sdtm_in[[refdt]], "%Y-%m-%d"), + error = function(e) { + warning("Encountered errors when converting refdt to dates. \n") + warning(e) + sdtm_in[refdt] <- NA + } + ) + tryCatch( + sdtm_in[[tgdt]] <- as.Date(sdtm_in[[tgdt]], "%Y-%m-%d"), + error = function(e) { + warning("Encountered errors when converting tgdt to dates. \n") + warning(e) + sdtm_in[tgdt] <- NA + } + ) + refdt_vector <- sdtm_in[[refdt]] tgdt_vector <- sdtm_in[[tgdt]] From 4cdf8144de140a4fbb1c0b699f66377f7b5fcea6 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 21 Jan 2024 22:45:09 +0000 Subject: [PATCH 10/66] utils::hasName --- R/calculate_study_day.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index bcf61994..668af849 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -47,10 +47,10 @@ calculate_study_day <- function(sdtm_in, assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(dm_domain)) - assertthat::assert_that(hasName(dm_domain, refdt)) - assertthat::assert_that(hasName(sdtm_in, tgdt)) - assertthat::assert_that(hasName(dm_domain, merge_key)) - assertthat::assert_that(hasName(sdtm_in, merge_key)) + assertthat::assert_that(utils::hasName(dm_domain, refdt)) + assertthat::assert_that(utils::hasName(sdtm_in, tgdt)) + assertthat::assert_that(utils::hasName(dm_domain, merge_key)) + assertthat::assert_that(utils::hasName(sdtm_in, merge_key)) assertthat::assert_that(is.character(study_day_var)) # check tgdt and study_day_var matching, for example, CMSTDTC matches CMSTDY if (gsub("DTC", "", tgdt) != gsub("DY", "", study_day_var)) { From e62d5dc362e9bebb71f68e9238a88b149ec49f0d Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 21 Jan 2024 23:02:59 +0000 Subject: [PATCH 11/66] modify assert message --- R/calculate_study_day.R | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 668af849..57c0c056 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -21,6 +21,8 @@ #' @return a data.frame that takes all columns from `sdtm_in` and a new variable #' to represent the calculated study day. #' +#' @export +#' #' @examples #' \dontrun{ #' ae <- data.frame( @@ -47,10 +49,22 @@ calculate_study_day <- function(sdtm_in, assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(dm_domain)) - assertthat::assert_that(utils::hasName(dm_domain, refdt)) - assertthat::assert_that(utils::hasName(sdtm_in, tgdt)) - assertthat::assert_that(utils::hasName(dm_domain, merge_key)) - assertthat::assert_that(utils::hasName(sdtm_in, merge_key)) + assertthat::assert_that( + utils::hasName(dm_domain, refdt), + msg = "dm_domain needs to have the variable of refdt." + ) + assertthat::assert_that( + utils::hasName(sdtm_in, tgdt), + msg = "sdtm_in needs to have the variable of tgdt." + ) + assertthat::assert_that( + utils::hasName(dm_domain, merge_key), + msg = "dm_domain needs to have the variable of merge_key." + ) + assertthat::assert_that( + utils::hasName(sdtm_in, merge_key), + msg = "sdtm_in needs to have the variable of merge_key." + ) assertthat::assert_that(is.character(study_day_var)) # check tgdt and study_day_var matching, for example, CMSTDTC matches CMSTDY if (gsub("DTC", "", tgdt) != gsub("DY", "", study_day_var)) { From 1c267e14a2d5a111c98f0b270e424352761c9b9a Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 22 Jan 2024 04:52:36 +0000 Subject: [PATCH 12/66] update message for refdt and tgdt convertion error --- R/calculate_study_day.R | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 57c0c056..6afa8c77 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -98,20 +98,26 @@ calculate_study_day <- function(sdtm_in, } # refdt/tgdt should be in ISO format, otherwise throw warning - tryCatch( - sdtm_in[[refdt]] <- as.Date(sdtm_in[[refdt]], "%Y-%m-%d"), + sdtm_in[[refdt]] <- tryCatch( + as.Date(sdtm_in[[refdt]], "%Y-%m-%d"), error = function(e) { - warning("Encountered errors when converting refdt to dates. \n") - warning(e) - sdtm_in[refdt] <- NA + warning( + "Encountered errors when converting refdt to dates. ", + "The warning message is ", + e$message + ) + NA } ) - tryCatch( - sdtm_in[[tgdt]] <- as.Date(sdtm_in[[tgdt]], "%Y-%m-%d"), + sdtm_in[[tgdt]] <- tryCatch( + as.Date(sdtm_in[[tgdt]], "%Y-%m-%d"), error = function(e) { - warning("Encountered errors when converting tgdt to dates. \n") - warning(e) - sdtm_in[tgdt] <- NA + warning( + "Encountered errors when converting tgdt to dates. ", + "The warning message is ", + e$message + ) + NA } ) From aecf81d90c7040e303df9735d2e11f1af0876127 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 22 Jan 2024 05:03:58 +0000 Subject: [PATCH 13/66] set .call to FALSE for warning --- R/calculate_study_day.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 6afa8c77..9a84ca23 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -74,6 +74,8 @@ calculate_study_day <- function(sdtm_in, ) } + original_variables <- names(sdtm_in) + if (!identical(sdtm_in, dm_domain)) { dm_domain <- unique(dm_domain[c(merge_key, refdt)]) @@ -104,7 +106,8 @@ calculate_study_day <- function(sdtm_in, warning( "Encountered errors when converting refdt to dates. ", "The warning message is ", - e$message + e$message, + call. = FALSE ) NA } @@ -115,7 +118,8 @@ calculate_study_day <- function(sdtm_in, warning( "Encountered errors when converting tgdt to dates. ", "The warning message is ", - e$message + e$message, + call. = FALSE ) NA } @@ -133,6 +137,8 @@ calculate_study_day <- function(sdtm_in, no = NA ) ) + + sdtm_in <- sdtm_in[original_variables] sdtm_in[study_day_var] <- res return(sdtm_in) } From 222c4b7449846ad67f3cdd91427ff31d00351c78 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 22 Jan 2024 05:07:59 +0000 Subject: [PATCH 14/66] add unit test --- tests/testthat/test-calculate_study_day.R | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/testthat/test-calculate_study_day.R diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R new file mode 100644 index 00000000..69297edd --- /dev/null +++ b/tests/testthat/test-calculate_study_day.R @@ -0,0 +1,63 @@ +AE <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") +) +DM <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + RFSTDTC = c("2012-02-01", "2012-04-14", NA_character_) +) + +test_that("`calculate_study_day()` works as epxected for bad input", { + expect_error( + calculate_study_day("a", "b", "C", "D", "E"), + "sdtm_in is not a data frame" + ) + expect_error( + calculate_study_day(iris, "b", "C", "D", "E"), + "dm_domain is not a data frame" + ) + expect_error( + calculate_study_day(iris, iris, "c", "d", "e"), + "dm_domain needs to have the variable of refdt" + ) + expect_error( + calculate_study_day(iris, iris, "Species", "d", "e"), + "sdtm_in needs to have the variable of tgdt" + ) + expect_error( + calculate_study_day(iris, iris, "Species", "Petal.Length", "e"), + "needs to have the variable of merge_key" + ) + expect_error( + calculate_study_day(iris, iris, "Species", "Petal.Length", 123, "Species"), + "study_day_var is not a character vector" + ) + expect_warning( + calculate_study_day(AE, DM, "RFSTDTC", "AESTDTC", "AENDY"), + "Target date and the returned study day doesn't match." + ) + + DM1 <- data.frame( + USUBJID = c("study123-123", "study123-123", "study123-125"), + RFSTDTC = c("2012-02-01", "2012-04-14", "2012-04-14") + ) + expect_warning( + calculate_study_day(AE, DM1, "RFSTDTC", "AESTDTC", "AESTDY"), + "Reference date is not unique for each patient!" + ) + + DM2 <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + RFSTDTC = c(123, 456, 789) + ) + expect_warning( + calculate_study_day(AE, DM2, "RFSTDTC", "AESTDTC", "AESTDY"), + "Encountered errors when converting refdt to dates." + ) +}) + +test_that("`calculate_study_day()` works as epxected for good input", { + res <- calculate_study_day(AE, DM, "RFSTDTC", "AESTDTC", "AESTDY") + expected <- c(-31, 1, NA) + expect_equal(res$AESTDY, expected) +}) From b936cf17d6e47eed3d2079080468dc84d33edb09 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 22 Jan 2024 05:08:39 +0000 Subject: [PATCH 15/66] update document --- NAMESPACE | 1 + man/calculate_study_day.Rd | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 455b5386..4651ec2a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(calculate_study_day) export(create_iso8601) export(fmt_cmp) importFrom(rlang,.data) diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index ec71af05..03b2b8cb 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -5,33 +5,33 @@ \title{\code{calculate_study_day} performs study day calculation} \usage{ calculate_study_day( - ds_in, - ds_dm, - refdt, + sdtm_in, + dm_domain = DM, + refdt = "RFSTDTC", tgdt, study_day_var, merge_key = "USUBJID" ) } \arguments{ -\item{ds_in}{input data.frame that contains the target date.} +\item{sdtm_in}{input data.frame that contains the target date.} -\item{ds_dm}{reference date.frame that contains the reference date.} +\item{dm_domain}{reference date.frame that contains the reference date.} -\item{refdt}{reference date from \code{ds_dm} that will be used as reference to +\item{refdt}{reference date from \code{dm_domain} that will be used as reference to calculate the study day.} -\item{tgdt}{target date from \code{ds_in} that will be used to calcualte the study +\item{tgdt}{target date from \code{sdtm_in} that will be used to calcualte the study day.} \item{study_day_var}{the new study day variable name in the output. For example, AESTDY for AE domain for CMSTDY for CM domain.} -\item{merge_key}{character to represents the merging key between \code{ds_in} and -\code{ds_dm}.} +\item{merge_key}{character to represents the merging key between \code{sdtm_in} and +\code{dm_domain}.} } \value{ -a data.frame that takes all columns from \code{ds_in} and a new variable +a data.frame that takes all columns from \code{sdtm_in} and a new variable to represent the calculated study day. } \description{ From 3cca05a7a1e5d8538cf5d95aabb78794a8454b5a Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 22 Jan 2024 05:18:47 +0000 Subject: [PATCH 16/66] style update --- R/calculate_study_day.R | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 9a84ca23..dfa8057a 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -25,28 +25,25 @@ #' #' @examples #' \dontrun{ -#' ae <- data.frame( -#' USUBJID = c("study123-123", "study123-124", "study123-125"), -#' AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") -#' ) -#' dm <- data.frame( -#' USUBJID = c("study123-123", "study123-124", "study123-125"), -#' RFSTDTC = c("2012-02-01", "2012-04-14", NA) -#' ) -#' ae$AESTDTC <- as.Date(ae$AESTDTC) -#' dm$RFSTDTC <- as.Date(dm$RFSTDTC) -#' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") +#' ae <- data.frame( +#' USUBJID = c("study123-123", "study123-124", "study123-125"), +#' AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") +#' ) +#' dm <- data.frame( +#' USUBJID = c("study123-123", "study123-124", "study123-125"), +#' RFSTDTC = c("2012-02-01", "2012-04-14", NA) +#' ) +#' ae$AESTDTC <- as.Date(ae$AESTDTC) +#' dm$RFSTDTC <- as.Date(dm$RFSTDTC) +#' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") #' } #' -#' - calculate_study_day <- function(sdtm_in, dm_domain = DM, refdt = "RFSTDTC", tgdt, study_day_var, merge_key = "USUBJID") { - assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(dm_domain)) assertthat::assert_that( @@ -80,7 +77,7 @@ calculate_study_day <- function(sdtm_in, dm_domain <- unique(dm_domain[c(merge_key, refdt)]) check_refdt_uniqueness <- dm_domain %>% - dplyr::group_by(dplyr::pick({{merge_key}})) %>% + dplyr::group_by(dplyr::pick({{ merge_key }})) %>% dplyr::filter(dplyr::n() > 1) if (nrow(check_refdt_uniqueness) > 0) { warning( @@ -95,7 +92,8 @@ calculate_study_day <- function(sdtm_in, sdtm_in <- sdtm_in %>% dplyr::left_join( - dm_domain, by = structure(names = merge_key, .Data = merge_key) + dm_domain, + by = structure(names = merge_key, .Data = merge_key) ) } @@ -142,4 +140,3 @@ calculate_study_day <- function(sdtm_in, sdtm_in[study_day_var] <- res return(sdtm_in) } - From 5e41b4b80143ea50a4853a94848ec34cc9df811c Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 22 Jan 2024 05:20:25 +0000 Subject: [PATCH 17/66] add asserthat in DESCRIPTION --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7d93b1f5..be9020b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,8 @@ Imports: purrr (>= 1.0.0), rlang (>= 1.0.0), stringr, - tibble + tibble, + assertthat Suggests: knitr, rmarkdown, From a4fc26993a73d53985c6b947054d265b159e3953 Mon Sep 17 00:00:00 2001 From: yli110-stat697 Date: Mon, 22 Jan 2024 05:24:14 +0000 Subject: [PATCH 18/66] Automatic renv profile update. --- renv/profiles/4.2/renv.lock | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/renv/profiles/4.2/renv.lock b/renv/profiles/4.2/renv.lock index 4120eeca..67f4a686 100644 --- a/renv/profiles/4.2/renv.lock +++ b/renv/profiles/4.2/renv.lock @@ -98,6 +98,16 @@ ], "Hash": "e8a22846fff485f0be3770c2da758713" }, + "assertthat": { + "Package": "assertthat", + "Version": "0.2.1", + "Source": "Repository", + "Repository": "RSPM", + "Requirements": [ + "tools" + ], + "Hash": "50c838a310445e954bc13f26f26a6ecf" + }, "backports": { "Package": "backports", "Version": "1.4.1", From 2e04f13a428349cba7949c64f0bcd289c8fc0b5b Mon Sep 17 00:00:00 2001 From: yli110-stat697 Date: Mon, 22 Jan 2024 05:27:33 +0000 Subject: [PATCH 19/66] Automatic renv profile update. --- renv.lock | 10 ++++++++++ renv/profiles/4.3/renv.lock | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/renv.lock b/renv.lock index 5141d10f..008e6742 100644 --- a/renv.lock +++ b/renv.lock @@ -98,6 +98,16 @@ ], "Hash": "e8a22846fff485f0be3770c2da758713" }, + "assertthat": { + "Package": "assertthat", + "Version": "0.2.1", + "Source": "Repository", + "Repository": "RSPM", + "Requirements": [ + "tools" + ], + "Hash": "50c838a310445e954bc13f26f26a6ecf" + }, "backports": { "Package": "backports", "Version": "1.4.1", diff --git a/renv/profiles/4.3/renv.lock b/renv/profiles/4.3/renv.lock index 5141d10f..008e6742 100644 --- a/renv/profiles/4.3/renv.lock +++ b/renv/profiles/4.3/renv.lock @@ -98,6 +98,16 @@ ], "Hash": "e8a22846fff485f0be3770c2da758713" }, + "assertthat": { + "Package": "assertthat", + "Version": "0.2.1", + "Source": "Repository", + "Repository": "RSPM", + "Requirements": [ + "tools" + ], + "Hash": "50c838a310445e954bc13f26f26a6ecf" + }, "backports": { "Package": "backports", "Version": "1.4.1", From 72641d19b4ac424233ff1297dc831509ce6b9ffd Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:53:41 -0600 Subject: [PATCH 20/66] use data frame instead of data.frame Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index dfa8057a..3f0ef0f0 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -1,6 +1,6 @@ #' `calculate_study_day` performs study day calculation #' @description -#' This function takes the an input data.frame and a reference data.frame (which +#' This function takes the an input data frame and a reference data frame (which #' is DM domain in most cases), and calculate the study day from reference date #' and target date. In case of unexpected conditions like reference date is not #' unique for each patient, or reference and input dates are not actual dates, From 8036644a1aa8ee186ebd6b44c4241fa10a1f3515 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:54:27 -0600 Subject: [PATCH 21/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 3f0ef0f0..78b1e51c 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -7,7 +7,7 @@ #' NA will be returned for those records. #' #' @md -#' @param sdtm_in input data.frame that contains the target date. +#' @param sdtm_in Input data frame that contains the target date. #' @param dm_domain reference date.frame that contains the reference date. #' @param refdt reference date from `dm_domain` that will be used as reference to #' calculate the study day. From d05a14764b66cffab16fe458c833253235560ce0 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:54:44 -0600 Subject: [PATCH 22/66] data frame instead of data.frame Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 78b1e51c..d710fa0d 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -8,7 +8,7 @@ #' #' @md #' @param sdtm_in Input data frame that contains the target date. -#' @param dm_domain reference date.frame that contains the reference date. +#' @param dm_domain Reference date frame that contains the reference date. #' @param refdt reference date from `dm_domain` that will be used as reference to #' calculate the study day. #' @param tgdt target date from `sdtm_in` that will be used to calcualte the study From c49517054b22ba5cf8c44824a14d22df87eacea5 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:54:54 -0600 Subject: [PATCH 23/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index d710fa0d..46a59b1f 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -9,7 +9,7 @@ #' @md #' @param sdtm_in Input data frame that contains the target date. #' @param dm_domain Reference date frame that contains the reference date. -#' @param refdt reference date from `dm_domain` that will be used as reference to +#' @param refdt Reference date from `dm_domain` that will be used as reference to #' calculate the study day. #' @param tgdt target date from `sdtm_in` that will be used to calcualte the study #' day. From fa6667a52b898954685471439edb3fafae3b5ee3 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:55:05 -0600 Subject: [PATCH 24/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 46a59b1f..4a6e5485 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -11,7 +11,7 @@ #' @param dm_domain Reference date frame that contains the reference date. #' @param refdt Reference date from `dm_domain` that will be used as reference to #' calculate the study day. -#' @param tgdt target date from `sdtm_in` that will be used to calcualte the study +#' @param tgdt Target date from `sdtm_in` that will be used to calculate the study #' day. #' @param study_day_var the new study day variable name in the output. For #' example, AESTDY for AE domain for CMSTDY for CM domain. From 6d19981662d0729fef33024b51c843bf20b8fe0e Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:55:24 -0600 Subject: [PATCH 25/66] update wording for test Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- tests/testthat/test-calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 69297edd..42ecf758 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -7,7 +7,7 @@ DM <- data.frame( RFSTDTC = c("2012-02-01", "2012-04-14", NA_character_) ) -test_that("`calculate_study_day()` works as epxected for bad input", { +test_that("`calculate_study_day()` works as expected for invalid input", { expect_error( calculate_study_day("a", "b", "C", "D", "E"), "sdtm_in is not a data frame" From a0c6522322f046e92d8b180973504a613a747c34 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:55:35 -0600 Subject: [PATCH 26/66] update wording for test Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- tests/testthat/test-calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 42ecf758..98347242 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -56,7 +56,7 @@ test_that("`calculate_study_day()` works as expected for invalid input", { ) }) -test_that("`calculate_study_day()` works as epxected for good input", { +test_that("`calculate_study_day()` works as expected for valid input", { res <- calculate_study_day(AE, DM, "RFSTDTC", "AESTDTC", "AESTDY") expected <- c(-31, 1, NA) expect_equal(res$AESTDY, expected) From a90210f84f8cc9ee74382a4613f42f3333858bd4 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:55:51 -0600 Subject: [PATCH 27/66] use base pipe Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 4a6e5485..2d2c17f3 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -76,7 +76,7 @@ calculate_study_day <- function(sdtm_in, if (!identical(sdtm_in, dm_domain)) { dm_domain <- unique(dm_domain[c(merge_key, refdt)]) - check_refdt_uniqueness <- dm_domain %>% + check_refdt_uniqueness <- dm_domain |> dplyr::group_by(dplyr::pick({{ merge_key }})) %>% dplyr::filter(dplyr::n() > 1) if (nrow(check_refdt_uniqueness) > 0) { From 79799db47ea1a69501c44b5743a26ecc31f22b0c Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:56:02 -0600 Subject: [PATCH 28/66] use base pipe Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 2d2c17f3..653f7e72 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -77,7 +77,7 @@ calculate_study_day <- function(sdtm_in, dm_domain <- unique(dm_domain[c(merge_key, refdt)]) check_refdt_uniqueness <- dm_domain |> - dplyr::group_by(dplyr::pick({{ merge_key }})) %>% + dplyr::group_by(dplyr::pick({{ merge_key }})) |> dplyr::filter(dplyr::n() > 1) if (nrow(check_refdt_uniqueness) > 0) { warning( From 381de67a47543d3a3d9d50ce7a6853c02409224f Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:56:11 -0600 Subject: [PATCH 29/66] use base pipe Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 653f7e72..5f7b27de 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -90,7 +90,7 @@ calculate_study_day <- function(sdtm_in, ] } - sdtm_in <- sdtm_in %>% + sdtm_in <- sdtm_in |> dplyr::left_join( dm_domain, by = structure(names = merge_key, .Data = merge_key) From c3ec9cef0264413fa0ec5d6867c5a49c67a4c7aa Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 11 Feb 2024 17:45:19 +0000 Subject: [PATCH 30/66] update man file --- man/calculate_study_day.Rd | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index 03b2b8cb..b5f44f86 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -14,14 +14,14 @@ calculate_study_day( ) } \arguments{ -\item{sdtm_in}{input data.frame that contains the target date.} +\item{sdtm_in}{Input data frame that contains the target date.} -\item{dm_domain}{reference date.frame that contains the reference date.} +\item{dm_domain}{Reference date frame that contains the reference date.} -\item{refdt}{reference date from \code{dm_domain} that will be used as reference to +\item{refdt}{Reference date from \code{dm_domain} that will be used as reference to calculate the study day.} -\item{tgdt}{target date from \code{sdtm_in} that will be used to calcualte the study +\item{tgdt}{Target date from \code{sdtm_in} that will be used to calculate the study day.} \item{study_day_var}{the new study day variable name in the output. For @@ -35,7 +35,7 @@ a data.frame that takes all columns from \code{sdtm_in} and a new variable to represent the calculated study day. } \description{ -This function takes the an input data.frame and a reference data.frame (which +This function takes the an input data frame and a reference data frame (which is DM domain in most cases), and calculate the study day from reference date and target date. In case of unexpected conditions like reference date is not unique for each patient, or reference and input dates are not actual dates, @@ -43,18 +43,17 @@ NA will be returned for those records. } \examples{ \dontrun{ - ae <- data.frame( - USUBJID = c("study123-123", "study123-124", "study123-125"), - AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") - ) - dm <- data.frame( - USUBJID = c("study123-123", "study123-124", "study123-125"), - RFSTDTC = c("2012-02-01", "2012-04-14", NA) - ) - ae$AESTDTC <- as.Date(ae$AESTDTC) - dm$RFSTDTC <- as.Date(dm$RFSTDTC) - calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") +ae <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") +) +dm <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + RFSTDTC = c("2012-02-01", "2012-04-14", NA) +) +ae$AESTDTC <- as.Date(ae$AESTDTC) +dm$RFSTDTC <- as.Date(dm$RFSTDTC) +calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") } - } From 01c3fc50d6e22de3424120cd36945b1134a6977f Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 11 Feb 2024 22:50:42 +0000 Subject: [PATCH 31/66] update df name for lint compliance --- tests/testthat/test-calculate_study_day.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 98347242..05234f9d 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -1,8 +1,8 @@ -AE <- data.frame( +ae <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") ) -DM <- data.frame( +dm <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), RFSTDTC = c("2012-02-01", "2012-04-14", NA_character_) ) @@ -33,31 +33,31 @@ test_that("`calculate_study_day()` works as expected for invalid input", { "study_day_var is not a character vector" ) expect_warning( - calculate_study_day(AE, DM, "RFSTDTC", "AESTDTC", "AENDY"), + calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AENDY"), "Target date and the returned study day doesn't match." ) - DM1 <- data.frame( + dm1 <- data.frame( USUBJID = c("study123-123", "study123-123", "study123-125"), RFSTDTC = c("2012-02-01", "2012-04-14", "2012-04-14") ) expect_warning( - calculate_study_day(AE, DM1, "RFSTDTC", "AESTDTC", "AESTDY"), + calculate_study_day(ae, dm1, "RFSTDTC", "AESTDTC", "AESTDY"), "Reference date is not unique for each patient!" ) - DM2 <- data.frame( + dm2 <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), RFSTDTC = c(123, 456, 789) ) expect_warning( - calculate_study_day(AE, DM2, "RFSTDTC", "AESTDTC", "AESTDY"), + calculate_study_day(ae, dm2, "RFSTDTC", "AESTDTC", "AESTDY"), "Encountered errors when converting refdt to dates." ) }) test_that("`calculate_study_day()` works as expected for valid input", { - res <- calculate_study_day(AE, DM, "RFSTDTC", "AESTDTC", "AESTDY") + res <- calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") expected <- c(-31, 1, NA) expect_equal(res$AESTDY, expected) }) From 7f4a8bb5bc889b964508066d43bcd6ef4e88cf06 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 11 Feb 2024 22:54:03 +0000 Subject: [PATCH 32/66] integer with L --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 5f7b27de..f2f66fb4 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -128,7 +128,7 @@ calculate_study_day <- function(sdtm_in, res <- ifelse( test = refdt_vector <= tgdt_vector, - yes = refdt_vector - tgdt_vector + 1, + yes = refdt_vector - tgdt_vector + 1L, no = ifelse( test = refdt_vector > tgdt_vector, yes = tgdt_vector - refdt_vector, From a47e82764cda42bef9638e83922729eb8d4da939 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 11 Feb 2024 22:59:18 +0000 Subject: [PATCH 33/66] L for test integers --- tests/testthat/test-calculate_study_day.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 05234f9d..07d1fe92 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -29,7 +29,7 @@ test_that("`calculate_study_day()` works as expected for invalid input", { "needs to have the variable of merge_key" ) expect_error( - calculate_study_day(iris, iris, "Species", "Petal.Length", 123, "Species"), + calculate_study_day(iris, iris, "Species", "Petal.Length", 123L, "Species"), "study_day_var is not a character vector" ) expect_warning( @@ -48,7 +48,7 @@ test_that("`calculate_study_day()` works as expected for invalid input", { dm2 <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), - RFSTDTC = c(123, 456, 789) + RFSTDTC = c(123L, 456L, 789L) ) expect_warning( calculate_study_day(ae, dm2, "RFSTDTC", "AESTDTC", "AESTDY"), From f6d467bd015993b4e98869959c1f8c4e793b0578 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Sun, 11 Feb 2024 23:29:25 +0000 Subject: [PATCH 34/66] replace nested ifelse with case when per lintr suggestion --- R/calculate_study_day.R | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index f2f66fb4..49fcf1c6 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -123,20 +123,14 @@ calculate_study_day <- function(sdtm_in, } ) - refdt_vector <- sdtm_in[[refdt]] - tgdt_vector <- sdtm_in[[tgdt]] - - res <- ifelse( - test = refdt_vector <= tgdt_vector, - yes = refdt_vector - tgdt_vector + 1L, - no = ifelse( - test = refdt_vector > tgdt_vector, - yes = tgdt_vector - refdt_vector, - no = NA + sdtm_in <- sdtm_in |> dplyr::mutate( + {{ study_day_var }} := dplyr::case_when( + {{ refdt_vector }} <= {{ tgdt_vector }} ~ {{ refdt_vector }} - {{ tgdt_vector }} + 1, + {{ refdt_vector }} > {{ tgdt_vector }} ~ {{ tgdt_vector }} - {{ refdt_vector }}, + .default = NA ) ) - sdtm_in <- sdtm_in[original_variables] - sdtm_in[study_day_var] <- res + sdtm_in <- sdtm_in[c(original_variables, study_day_var)] return(sdtm_in) } From 9ad8dafedb6669b6fcee1c36740cfe94787b2289 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 01:53:25 +0000 Subject: [PATCH 35/66] Revert "replace nested ifelse with case when per lintr suggestion" This reverts commit f6d467bd015993b4e98869959c1f8c4e793b0578. --- R/calculate_study_day.R | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 49fcf1c6..f2f66fb4 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -123,14 +123,20 @@ calculate_study_day <- function(sdtm_in, } ) - sdtm_in <- sdtm_in |> dplyr::mutate( - {{ study_day_var }} := dplyr::case_when( - {{ refdt_vector }} <= {{ tgdt_vector }} ~ {{ refdt_vector }} - {{ tgdt_vector }} + 1, - {{ refdt_vector }} > {{ tgdt_vector }} ~ {{ tgdt_vector }} - {{ refdt_vector }}, - .default = NA + refdt_vector <- sdtm_in[[refdt]] + tgdt_vector <- sdtm_in[[tgdt]] + + res <- ifelse( + test = refdt_vector <= tgdt_vector, + yes = refdt_vector - tgdt_vector + 1L, + no = ifelse( + test = refdt_vector > tgdt_vector, + yes = tgdt_vector - refdt_vector, + no = NA ) ) - sdtm_in <- sdtm_in[c(original_variables, study_day_var)] + sdtm_in <- sdtm_in[original_variables] + sdtm_in[study_day_var] <- res return(sdtm_in) } From a1af5be22e972bb110f13d47a5ae0ee660479e54 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Sun, 11 Feb 2024 19:56:02 -0600 Subject: [PATCH 36/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index f2f66fb4..53a6c8c5 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -13,7 +13,7 @@ #' calculate the study day. #' @param tgdt Target date from `sdtm_in` that will be used to calculate the study #' day. -#' @param study_day_var the new study day variable name in the output. For +#' @param study_day_var New study day variable name in the output. For #' example, AESTDY for AE domain for CMSTDY for CM domain. #' @param merge_key character to represents the merging key between `sdtm_in` and #' `dm_domain`. From 0c786855aaeebb682682599132ddd5273754aa66 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 01:57:21 +0000 Subject: [PATCH 37/66] update typo in roxygen header --- R/calculate_study_day.R | 2 +- man/calculate_study_day.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index f2f66fb4..d7a15445 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -14,7 +14,7 @@ #' @param tgdt Target date from `sdtm_in` that will be used to calculate the study #' day. #' @param study_day_var the new study day variable name in the output. For -#' example, AESTDY for AE domain for CMSTDY for CM domain. +#' example, AESTDY for AE domain and CMSTDY for CM domain. #' @param merge_key character to represents the merging key between `sdtm_in` and #' `dm_domain`. #' diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index b5f44f86..7911cf8b 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -25,7 +25,7 @@ calculate the study day.} day.} \item{study_day_var}{the new study day variable name in the output. For -example, AESTDY for AE domain for CMSTDY for CM domain.} +example, AESTDY for AE domain and CMSTDY for CM domain.} \item{merge_key}{character to represents the merging key between \code{sdtm_in} and \code{dm_domain}.} From f20ed8b3bf6b096ee2f0352996078bb09322120a Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Sun, 11 Feb 2024 20:02:28 -0600 Subject: [PATCH 38/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index d7a15445..1de5470e 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -15,7 +15,7 @@ #' day. #' @param study_day_var the new study day variable name in the output. For #' example, AESTDY for AE domain and CMSTDY for CM domain. -#' @param merge_key character to represents the merging key between `sdtm_in` and +#' @param merge_key Character to represents the merging key between `sdtm_in` and #' `dm_domain`. #' #' @return a data.frame that takes all columns from `sdtm_in` and a new variable From 9c26b032dbb834f45d1002c8ea6a8a3cc6da5428 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Sun, 11 Feb 2024 20:02:44 -0600 Subject: [PATCH 39/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 1de5470e..5a28dfb4 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -18,7 +18,7 @@ #' @param merge_key Character to represents the merging key between `sdtm_in` and #' `dm_domain`. #' -#' @return a data.frame that takes all columns from `sdtm_in` and a new variable +#' @return Data frame that takes all columns from `sdtm_in` and a new variable #' to represent the calculated study day. #' #' @export From 8a977b2a9baaedf71d4d16f898130c7993263f9c Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 02:04:20 +0000 Subject: [PATCH 40/66] remove don't run --- R/calculate_study_day.R | 2 -- man/calculate_study_day.Rd | 2 -- 2 files changed, 4 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index d7a15445..527f1dd2 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -24,7 +24,6 @@ #' @export #' #' @examples -#' \dontrun{ #' ae <- data.frame( #' USUBJID = c("study123-123", "study123-124", "study123-125"), #' AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") @@ -36,7 +35,6 @@ #' ae$AESTDTC <- as.Date(ae$AESTDTC) #' dm$RFSTDTC <- as.Date(dm$RFSTDTC) #' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") -#' } #' calculate_study_day <- function(sdtm_in, dm_domain = DM, diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index 7911cf8b..b695e5f5 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -42,7 +42,6 @@ unique for each patient, or reference and input dates are not actual dates, NA will be returned for those records. } \examples{ -\dontrun{ ae <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") @@ -54,6 +53,5 @@ dm <- data.frame( ae$AESTDTC <- as.Date(ae$AESTDTC) dm$RFSTDTC <- as.Date(dm$RFSTDTC) calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") -} } From 06532bd22cd515d8993162fe42d5f5139e4e4684 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Sun, 11 Feb 2024 20:05:57 -0600 Subject: [PATCH 41/66] capitalize param per reference Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index f50cbaa5..1d5113eb 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -13,7 +13,7 @@ #' calculate the study day. #' @param tgdt Target date from `sdtm_in` that will be used to calculate the study #' day. -#' @param study_day_var the new study day variable name in the output. For +#' @param study_day_var New study day variable name in the output. For #' example, AESTDY for AE domain and CMSTDY for CM domain. #' @param merge_key Character to represents the merging key between `sdtm_in` and #' `dm_domain`. From 0be0d8b7f9b627bb3ffa82bb0b765ff448bbb55d Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 03:24:55 +0000 Subject: [PATCH 42/66] use mapply to replace nested if else --- R/calculate_study_day.R | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index f50cbaa5..6235fcb8 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -124,15 +124,14 @@ calculate_study_day <- function(sdtm_in, refdt_vector <- sdtm_in[[refdt]] tgdt_vector <- sdtm_in[[tgdt]] - res <- ifelse( - test = refdt_vector <= tgdt_vector, - yes = refdt_vector - tgdt_vector + 1L, - no = ifelse( - test = refdt_vector > tgdt_vector, - yes = tgdt_vector - refdt_vector, - no = NA - ) - ) + dy_cal_func <- function(ref, tgt) { + if (is.na(ref) || is.na(tgt)) res <- NA + else if (ref <= tgt) res <- ref - tgt + 1L + else if (ref > tgt) res <- tgt - ref + else res <- NA + return(res) + } + res <- mapply(dy_cal_func, refdt_vector, tgdt_vector, SIMPLIFY = TRUE, USE.NAMES = FALSE) sdtm_in <- sdtm_in[original_variables] sdtm_in[study_day_var] <- res From c3d812ff17e2825ac43017afc107b0c8e06a463f Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 03:37:40 +0000 Subject: [PATCH 43/66] style --- R/calculate_study_day.R | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index ad2e5fa0..568f169b 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -125,10 +125,15 @@ calculate_study_day <- function(sdtm_in, tgdt_vector <- sdtm_in[[tgdt]] dy_cal_func <- function(ref, tgt) { - if (is.na(ref) || is.na(tgt)) res <- NA - else if (ref <= tgt) res <- ref - tgt + 1L - else if (ref > tgt) res <- tgt - ref - else res <- NA + if (is.na(ref) || is.na(tgt)) { + res <- NA + } else if (ref <= tgt) { + res <- ref - tgt + 1L + } else if (ref > tgt) { + res <- tgt - ref + } else { + res <- NA + } return(res) } res <- mapply(dy_cal_func, refdt_vector, tgdt_vector, SIMPLIFY = TRUE, USE.NAMES = FALSE) From 99cc8d2d3db51d94da43c464fa87c198c3ec3c5b Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 03:38:23 +0000 Subject: [PATCH 44/66] roxygen update --- man/calculate_study_day.Rd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index b695e5f5..5382e46a 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -24,14 +24,14 @@ calculate the study day.} \item{tgdt}{Target date from \code{sdtm_in} that will be used to calculate the study day.} -\item{study_day_var}{the new study day variable name in the output. For +\item{study_day_var}{New study day variable name in the output. For example, AESTDY for AE domain and CMSTDY for CM domain.} -\item{merge_key}{character to represents the merging key between \code{sdtm_in} and +\item{merge_key}{Character to represents the merging key between \code{sdtm_in} and \code{dm_domain}.} } \value{ -a data.frame that takes all columns from \code{sdtm_in} and a new variable +Data frame that takes all columns from \code{sdtm_in} and a new variable to represent the calculated study day. } \description{ From 92432684698bb082d20a28ae1a016e23368278b9 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 03:58:39 +0000 Subject: [PATCH 45/66] fixed to be TRUE --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 568f169b..7913f7b3 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -62,7 +62,7 @@ calculate_study_day <- function(sdtm_in, ) assertthat::assert_that(is.character(study_day_var)) # check tgdt and study_day_var matching, for example, CMSTDTC matches CMSTDY - if (gsub("DTC", "", tgdt) != gsub("DY", "", study_day_var)) { + if (gsub("DTC", "", tgdt, fixed = TRUE) != gsub("DY", "", study_day_var, fixed = TRUE)) { warning( "Target date and the returned study day doesn't match. ", "Expecting matching date and study day, for example, CMENDTC and CMENDY" From df4a7edf8300825fb8c0df3bd23ec83e273fa60c Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 04:01:41 +0000 Subject: [PATCH 46/66] fix lintr --- R/calculate_study_day.R | 4 ++-- tests/testthat/test-calculate_study_day.R | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 7913f7b3..28a59741 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -76,8 +76,8 @@ calculate_study_day <- function(sdtm_in, check_refdt_uniqueness <- dm_domain |> dplyr::group_by(dplyr::pick({{ merge_key }})) |> - dplyr::filter(dplyr::n() > 1) - if (nrow(check_refdt_uniqueness) > 0) { + dplyr::filter(dplyr::n() > 1L) + if (nrow(check_refdt_uniqueness) > 0L) { warning( "Reference date is not unique for each patient! ", "Patient without unique reference date will be ingored. ", diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 07d1fe92..3be65d7b 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -1,10 +1,12 @@ ae <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), - AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14") + AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14"), + stringsAsFactors = FALSE ) dm <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), - RFSTDTC = c("2012-02-01", "2012-04-14", NA_character_) + RFSTDTC = c("2012-02-01", "2012-04-14", NA_character_), + stringsAsFactors = FALSE ) test_that("`calculate_study_day()` works as expected for invalid input", { @@ -39,7 +41,8 @@ test_that("`calculate_study_day()` works as expected for invalid input", { dm1 <- data.frame( USUBJID = c("study123-123", "study123-123", "study123-125"), - RFSTDTC = c("2012-02-01", "2012-04-14", "2012-04-14") + RFSTDTC = c("2012-02-01", "2012-04-14", "2012-04-14"), + stringsAsFactors = FALSE ) expect_warning( calculate_study_day(ae, dm1, "RFSTDTC", "AESTDTC", "AESTDY"), @@ -48,7 +51,8 @@ test_that("`calculate_study_day()` works as expected for invalid input", { dm2 <- data.frame( USUBJID = c("study123-123", "study123-124", "study123-125"), - RFSTDTC = c(123L, 456L, 789L) + RFSTDTC = c(123L, 456L, 789L), + stringsAsFactors = FALSE ) expect_warning( calculate_study_day(ae, dm2, "RFSTDTC", "AESTDTC", "AESTDY"), From 4e7c3925edabe8fd9ceb0c1f03cdeb0c18e1136b Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 12 Feb 2024 04:21:29 +0000 Subject: [PATCH 47/66] fix lintr in test file --- tests/testthat/test-calculate_study_day.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 3be65d7b..6c13926b 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -62,6 +62,6 @@ test_that("`calculate_study_day()` works as expected for invalid input", { test_that("`calculate_study_day()` works as expected for valid input", { res <- calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") - expected <- c(-31, 1, NA) - expect_equal(res$AESTDY, expected) + expected <- c(-31L, 1L, NA) + expect_equal(res$AESTDY, expected, tolerance = "1.5e-08") }) From 026f5a3f13ab8936af9d006869fd8c0beb04e85d Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Thu, 15 Feb 2024 23:46:12 +0000 Subject: [PATCH 48/66] remove @md tag --- R/calculate_study_day.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 28a59741..2d11a548 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -6,7 +6,6 @@ #' unique for each patient, or reference and input dates are not actual dates, #' NA will be returned for those records. #' -#' @md #' @param sdtm_in Input data frame that contains the target date. #' @param dm_domain Reference date frame that contains the reference date. #' @param refdt Reference date from `dm_domain` that will be used as reference to From 7e92d53a5002b95fa5c55873a821d1dba5c86ef6 Mon Sep 17 00:00:00 2001 From: Ramiro Magno Date: Sat, 17 Feb 2024 02:07:32 +0000 Subject: [PATCH 49/66] Update `calculate_study_day()` Fix usage of default missing value in parameter `dm_domain` and calculate study day with more straightforward expression. --- R/calculate_study_day.R | 21 +++++---------------- man/calculate_study_day.Rd | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 2d11a548..36e3f6e2 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -36,7 +36,7 @@ #' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") #' calculate_study_day <- function(sdtm_in, - dm_domain = DM, + dm_domain, refdt = "RFSTDTC", tgdt, study_day_var, @@ -120,22 +120,11 @@ calculate_study_day <- function(sdtm_in, } ) - refdt_vector <- sdtm_in[[refdt]] - tgdt_vector <- sdtm_in[[tgdt]] + ref <- sdtm_in[[refdt]] + tgt <- sdtm_in[[tgdt]] - dy_cal_func <- function(ref, tgt) { - if (is.na(ref) || is.na(tgt)) { - res <- NA - } else if (ref <= tgt) { - res <- ref - tgt + 1L - } else if (ref > tgt) { - res <- tgt - ref - } else { - res <- NA - } - return(res) - } - res <- mapply(dy_cal_func, refdt_vector, tgdt_vector, SIMPLIFY = TRUE, USE.NAMES = FALSE) + # SDTMIG 4.4.4 Use of the Study Day Variables + res <- ifelse(tgt > ref, tgt - ref + 1L, tgt - ref) sdtm_in <- sdtm_in[original_variables] sdtm_in[study_day_var] <- res diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index 5382e46a..724b6dfe 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -6,7 +6,7 @@ \usage{ calculate_study_day( sdtm_in, - dm_domain = DM, + dm_domain, refdt = "RFSTDTC", tgdt, study_day_var, From d6510285f5f87c24fc71f30e8803e16e85563bd6 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 19 Feb 2024 15:32:04 +0000 Subject: [PATCH 50/66] move defaults to the end of argument positions --- R/calculate_study_day.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 2d11a548..68240b5f 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -36,10 +36,10 @@ #' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") #' calculate_study_day <- function(sdtm_in, - dm_domain = DM, - refdt = "RFSTDTC", tgdt, study_day_var, + dm_domain = "DM", + refdt = "RFSTDTC", merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(dm_domain)) From ae903e92e87002b2cd7cfd1c3ff77c71a4831b82 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 19 Feb 2024 15:52:08 +0000 Subject: [PATCH 51/66] add equal sign --- R/calculate_study_day.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index cf882dbc..afc3841a 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -36,8 +36,6 @@ #' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") #' calculate_study_day <- function(sdtm_in, - dm_domain, - refdt = "RFSTDTC", tgdt, study_day_var, dm_domain = "DM", @@ -126,7 +124,7 @@ calculate_study_day <- function(sdtm_in, tgt <- sdtm_in[[tgdt]] # SDTMIG 4.4.4 Use of the Study Day Variables - res <- ifelse(tgt > ref, tgt - ref + 1L, tgt - ref) + res <- ifelse(tgt >= ref, tgt - ref + 1L, tgt - ref) sdtm_in <- sdtm_in[original_variables] sdtm_in[study_day_var] <- res From 64dcdb16eb75d034fb33e48d24dd3cb29fa390ed Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 19 Feb 2024 16:02:40 +0000 Subject: [PATCH 52/66] default to "DM" --- R/calculate_study_day.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index afc3841a..dd4ee60c 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -33,7 +33,7 @@ #' ) #' ae$AESTDTC <- as.Date(ae$AESTDTC) #' dm$RFSTDTC <- as.Date(dm$RFSTDTC) -#' calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") +#' calculate_study_day(ae, "AESTDTC", "AESTDY", "dm", "RFSTDTC") #' calculate_study_day <- function(sdtm_in, tgdt, @@ -42,6 +42,7 @@ calculate_study_day <- function(sdtm_in, refdt = "RFSTDTC", merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(sdtm_in)) + dm_domain <- eval(parse(text = dm_domain)) assertthat::assert_that(is.data.frame(dm_domain)) assertthat::assert_that( utils::hasName(dm_domain, refdt), From 7c9172210c390c33f5dc55ccc01e5564a950b903 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 19 Feb 2024 16:10:50 +0000 Subject: [PATCH 53/66] update unit test per the arg position change --- tests/testthat/test-calculate_study_day.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 6c13926b..b52bae69 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -19,23 +19,23 @@ test_that("`calculate_study_day()` works as expected for invalid input", { "dm_domain is not a data frame" ) expect_error( - calculate_study_day(iris, iris, "c", "d", "e"), + calculate_study_day(iris, "d", "e", "iris", "b"), "dm_domain needs to have the variable of refdt" ) expect_error( - calculate_study_day(iris, iris, "Species", "d", "e"), + calculate_study_day(iris, "e", "d", "iris", "Species"), "sdtm_in needs to have the variable of tgdt" ) expect_error( - calculate_study_day(iris, iris, "Species", "Petal.Length", "e"), + calculate_study_day(iris, "Petal.Length", "e", "iris", "Species"), "needs to have the variable of merge_key" ) expect_error( - calculate_study_day(iris, iris, "Species", "Petal.Length", 123L, "Species"), + calculate_study_day(iris, "Petal.Length", 123L, "iris", "Species", "Species"), "study_day_var is not a character vector" ) expect_warning( - calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AENDY"), + calculate_study_day(ae, "AESTDTC", "AENDY", "dm", "RFSTDTC"), "Target date and the returned study day doesn't match." ) @@ -45,7 +45,7 @@ test_that("`calculate_study_day()` works as expected for invalid input", { stringsAsFactors = FALSE ) expect_warning( - calculate_study_day(ae, dm1, "RFSTDTC", "AESTDTC", "AESTDY"), + calculate_study_day(ae, "AESTDTC", "AESTDY", "dm1", "RFSTDTC"), "Reference date is not unique for each patient!" ) @@ -55,13 +55,13 @@ test_that("`calculate_study_day()` works as expected for invalid input", { stringsAsFactors = FALSE ) expect_warning( - calculate_study_day(ae, dm2, "RFSTDTC", "AESTDTC", "AESTDY"), + calculate_study_day(ae, "AESTDTC", "AESTDY", "dm2", "RFSTDTC"), "Encountered errors when converting refdt to dates." ) }) test_that("`calculate_study_day()` works as expected for valid input", { - res <- calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") + res <- calculate_study_day(ae, "AESTDTC", "AESTDY", "dm", "RFSTDTC") expected <- c(-31L, 1L, NA) expect_equal(res$AESTDY, expected, tolerance = "1.5e-08") }) From fa26e738c5b32006cc3da5c8a55b729fcb155844 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 19 Feb 2024 16:23:46 +0000 Subject: [PATCH 54/66] man file update --- R/calculate_study_day.R | 6 +++--- man/calculate_study_day.Rd | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index dd4ee60c..52d5b7f6 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -7,13 +7,13 @@ #' NA will be returned for those records. #' #' @param sdtm_in Input data frame that contains the target date. -#' @param dm_domain Reference date frame that contains the reference date. -#' @param refdt Reference date from `dm_domain` that will be used as reference to -#' calculate the study day. #' @param tgdt Target date from `sdtm_in` that will be used to calculate the study #' day. #' @param study_day_var New study day variable name in the output. For #' example, AESTDY for AE domain and CMSTDY for CM domain. +#' @param dm_domain Reference date frame that contains the reference date. +#' @param refdt Reference date from `dm_domain` that will be used as reference to +#' calculate the study day. #' @param merge_key Character to represents the merging key between `sdtm_in` and #' `dm_domain`. #' diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index 724b6dfe..201cfcab 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -6,27 +6,27 @@ \usage{ calculate_study_day( sdtm_in, - dm_domain, - refdt = "RFSTDTC", tgdt, study_day_var, + dm_domain = "DM", + refdt = "RFSTDTC", merge_key = "USUBJID" ) } \arguments{ \item{sdtm_in}{Input data frame that contains the target date.} -\item{dm_domain}{Reference date frame that contains the reference date.} - -\item{refdt}{Reference date from \code{dm_domain} that will be used as reference to -calculate the study day.} - \item{tgdt}{Target date from \code{sdtm_in} that will be used to calculate the study day.} \item{study_day_var}{New study day variable name in the output. For example, AESTDY for AE domain and CMSTDY for CM domain.} +\item{dm_domain}{Reference date frame that contains the reference date.} + +\item{refdt}{Reference date from \code{dm_domain} that will be used as reference to +calculate the study day.} + \item{merge_key}{Character to represents the merging key between \code{sdtm_in} and \code{dm_domain}.} } @@ -52,6 +52,6 @@ dm <- data.frame( ) ae$AESTDTC <- as.Date(ae$AESTDTC) dm$RFSTDTC <- as.Date(dm$RFSTDTC) -calculate_study_day(ae, dm, "RFSTDTC", "AESTDTC", "AESTDY") +calculate_study_day(ae, "AESTDTC", "AESTDY", "dm", "RFSTDTC") } From 958453ef64e762a056f244a644f1e9aafdb71d94 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Wed, 21 Feb 2024 19:09:48 +0000 Subject: [PATCH 55/66] remove dm_domain default --- R/calculate_study_day.R | 13 ++++++------- man/calculate_study_day.Rd | 16 ++++++++-------- tests/testthat/test-calculate_study_day.R | 16 ++++++++-------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 52d5b7f6..2ad107ba 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -7,13 +7,13 @@ #' NA will be returned for those records. #' #' @param sdtm_in Input data frame that contains the target date. +#' @param dm_domain Reference date frame that contains the reference date. #' @param tgdt Target date from `sdtm_in` that will be used to calculate the study #' day. -#' @param study_day_var New study day variable name in the output. For -#' example, AESTDY for AE domain and CMSTDY for CM domain. -#' @param dm_domain Reference date frame that contains the reference date. #' @param refdt Reference date from `dm_domain` that will be used as reference to #' calculate the study day. +#' @param study_day_var New study day variable name in the output. For +#' example, AESTDY for AE domain and CMSTDY for CM domain. #' @param merge_key Character to represents the merging key between `sdtm_in` and #' `dm_domain`. #' @@ -33,16 +33,15 @@ #' ) #' ae$AESTDTC <- as.Date(ae$AESTDTC) #' dm$RFSTDTC <- as.Date(dm$RFSTDTC) -#' calculate_study_day(ae, "AESTDTC", "AESTDY", "dm", "RFSTDTC") +#' calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") #' calculate_study_day <- function(sdtm_in, + dm_domain, tgdt, + refdt, study_day_var, - dm_domain = "DM", - refdt = "RFSTDTC", merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(sdtm_in)) - dm_domain <- eval(parse(text = dm_domain)) assertthat::assert_that(is.data.frame(dm_domain)) assertthat::assert_that( utils::hasName(dm_domain, refdt), diff --git a/man/calculate_study_day.Rd b/man/calculate_study_day.Rd index 201cfcab..43e219b4 100644 --- a/man/calculate_study_day.Rd +++ b/man/calculate_study_day.Rd @@ -6,27 +6,27 @@ \usage{ calculate_study_day( sdtm_in, + dm_domain, tgdt, + refdt, study_day_var, - dm_domain = "DM", - refdt = "RFSTDTC", merge_key = "USUBJID" ) } \arguments{ \item{sdtm_in}{Input data frame that contains the target date.} +\item{dm_domain}{Reference date frame that contains the reference date.} + \item{tgdt}{Target date from \code{sdtm_in} that will be used to calculate the study day.} -\item{study_day_var}{New study day variable name in the output. For -example, AESTDY for AE domain and CMSTDY for CM domain.} - -\item{dm_domain}{Reference date frame that contains the reference date.} - \item{refdt}{Reference date from \code{dm_domain} that will be used as reference to calculate the study day.} +\item{study_day_var}{New study day variable name in the output. For +example, AESTDY for AE domain and CMSTDY for CM domain.} + \item{merge_key}{Character to represents the merging key between \code{sdtm_in} and \code{dm_domain}.} } @@ -52,6 +52,6 @@ dm <- data.frame( ) ae$AESTDTC <- as.Date(ae$AESTDTC) dm$RFSTDTC <- as.Date(dm$RFSTDTC) -calculate_study_day(ae, "AESTDTC", "AESTDY", "dm", "RFSTDTC") +calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") } diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index b52bae69..a57546b2 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -19,23 +19,23 @@ test_that("`calculate_study_day()` works as expected for invalid input", { "dm_domain is not a data frame" ) expect_error( - calculate_study_day(iris, "d", "e", "iris", "b"), + calculate_study_day(iris, iris, "d", "e", "b"), "dm_domain needs to have the variable of refdt" ) expect_error( - calculate_study_day(iris, "e", "d", "iris", "Species"), + calculate_study_day(iris, iris, "d", "Species"), "sdtm_in needs to have the variable of tgdt" ) expect_error( - calculate_study_day(iris, "Petal.Length", "e", "iris", "Species"), + calculate_study_day(iris, iris, "Petal.Length", "Species", "e"), "needs to have the variable of merge_key" ) expect_error( - calculate_study_day(iris, "Petal.Length", 123L, "iris", "Species", "Species"), + calculate_study_day(iris, iris, "Petal.Length", "Species", 123L, "Species"), "study_day_var is not a character vector" ) expect_warning( - calculate_study_day(ae, "AESTDTC", "AENDY", "dm", "RFSTDTC"), + calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AENDY"), "Target date and the returned study day doesn't match." ) @@ -45,7 +45,7 @@ test_that("`calculate_study_day()` works as expected for invalid input", { stringsAsFactors = FALSE ) expect_warning( - calculate_study_day(ae, "AESTDTC", "AESTDY", "dm1", "RFSTDTC"), + calculate_study_day(ae, dm1, "AESTDTC", "RFSTDTC", "AESTDY"), "Reference date is not unique for each patient!" ) @@ -55,13 +55,13 @@ test_that("`calculate_study_day()` works as expected for invalid input", { stringsAsFactors = FALSE ) expect_warning( - calculate_study_day(ae, "AESTDTC", "AESTDY", "dm2", "RFSTDTC"), + calculate_study_day(ae, dm2, "AESTDTC", "RFSTDTC", "AESTDY"), "Encountered errors when converting refdt to dates." ) }) test_that("`calculate_study_day()` works as expected for valid input", { - res <- calculate_study_day(ae, "AESTDTC", "AESTDY", "dm", "RFSTDTC") + res <- calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") expected <- c(-31L, 1L, NA) expect_equal(res$AESTDY, expected, tolerance = "1.5e-08") }) From b95a82a4cbe06f7de03f9aaaf70c7fd19b31b04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Fory=C5=9B?= Date: Tue, 27 Feb 2024 09:52:02 +0100 Subject: [PATCH 56/66] Update common.yml --- .github/workflows/common.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/common.yml b/.github/workflows/common.yml index de646ffb..202066b0 100644 --- a/.github/workflows/common.yml +++ b/.github/workflows/common.yml @@ -90,7 +90,7 @@ jobs: if: > github.event_name != 'release' with: - r-version: "4.3" + r-version: "release" # Whether to skip code coverage badge creation # Setting to 'false' will require you to create # an orphan branch called 'badges' in your repository From 9f739b749746c5c98ebfe3c90d0812d754e6db81 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 14:23:21 +0000 Subject: [PATCH 57/66] simplify by statement --- R/calculate_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index 2ad107ba..f2cdd670 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -90,7 +90,7 @@ calculate_study_day <- function(sdtm_in, sdtm_in <- sdtm_in |> dplyr::left_join( dm_domain, - by = structure(names = merge_key, .Data = merge_key) + by = merge_key ) } From f424a5878fea50f15da216fb23e06de490ae9504 Mon Sep 17 00:00:00 2001 From: yli110-stat697 Date: Mon, 4 Mar 2024 14:34:55 +0000 Subject: [PATCH 58/66] Automatic renv profile update. --- renv.lock | 2 +- renv/profiles/4.3/renv.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/renv.lock b/renv.lock index 29929f28..329ec463 100644 --- a/renv.lock +++ b/renv.lock @@ -1,6 +1,6 @@ { "R": { - "Version": "4.3.2", + "Version": "4.3.3", "Repositories": [ { "Name": "CRAN", diff --git a/renv/profiles/4.3/renv.lock b/renv/profiles/4.3/renv.lock index 29929f28..329ec463 100644 --- a/renv/profiles/4.3/renv.lock +++ b/renv/profiles/4.3/renv.lock @@ -1,6 +1,6 @@ { "R": { - "Version": "4.3.2", + "Version": "4.3.3", "Repositories": [ { "Name": "CRAN", From d57ad5aa42253a2fd31a2573fc946520677e7e96 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 14:38:11 +0000 Subject: [PATCH 59/66] add unit test for same input and reference df --- tests/testthat/test-calculate_study_day.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index a57546b2..599579a7 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -64,4 +64,13 @@ test_that("`calculate_study_day()` works as expected for valid input", { res <- calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") expected <- c(-31L, 1L, NA) expect_equal(res$AESTDY, expected, tolerance = "1.5e-08") + + df <- data.frame( + USUBJID = c("study123-123", "study123-124", "study123-125"), + RFSTDTC = c("2012-02-01", "2012-04-14", NA_character_), + AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14"), + stringsAsFactors = FALSE + ) + res1 <- calculate_study_day(df, df, "AESTDTC", "RFSTDTC", "AESTDY") + expect_equal(res1$AESTDY, expected, tolerance = "1.5e-08") }) From 091e5060b6d84075376098c28aae18f614358bca Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 14:54:53 +0000 Subject: [PATCH 60/66] rename function --- NAMESPACE | 2 +- R/calculate_study_day.R | 16 ++++++------- ...ulate_study_day.Rd => derive_study_day.Rd} | 10 ++++---- tests/testthat/test-calculate_study_day.R | 24 +++++++++---------- 4 files changed, 26 insertions(+), 26 deletions(-) rename man/{calculate_study_day.Rd => derive_study_day.Rd} (88%) diff --git a/NAMESPACE b/NAMESPACE index 5a20dbf7..f1161f5e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,8 @@ # Generated by roxygen2: do not edit by hand S3method(print,iso8601) -export(calculate_study_day) export(create_iso8601) +export(derive_study_day) export(fmt_cmp) export(problems) importFrom(rlang,.data) diff --git a/R/calculate_study_day.R b/R/calculate_study_day.R index f2cdd670..bac83d5d 100644 --- a/R/calculate_study_day.R +++ b/R/calculate_study_day.R @@ -1,4 +1,4 @@ -#' `calculate_study_day` performs study day calculation +#' `derive_study_day` performs study day calculation #' @description #' This function takes the an input data frame and a reference data frame (which #' is DM domain in most cases), and calculate the study day from reference date @@ -33,14 +33,14 @@ #' ) #' ae$AESTDTC <- as.Date(ae$AESTDTC) #' dm$RFSTDTC <- as.Date(dm$RFSTDTC) -#' calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") +#' derive_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") #' -calculate_study_day <- function(sdtm_in, - dm_domain, - tgdt, - refdt, - study_day_var, - merge_key = "USUBJID") { +derive_study_day <- function(sdtm_in, + dm_domain, + tgdt, + refdt, + study_day_var, + merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(dm_domain)) assertthat::assert_that( diff --git a/man/calculate_study_day.Rd b/man/derive_study_day.Rd similarity index 88% rename from man/calculate_study_day.Rd rename to man/derive_study_day.Rd index 43e219b4..d8352b9d 100644 --- a/man/calculate_study_day.Rd +++ b/man/derive_study_day.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/calculate_study_day.R -\name{calculate_study_day} -\alias{calculate_study_day} -\title{\code{calculate_study_day} performs study day calculation} +\name{derive_study_day} +\alias{derive_study_day} +\title{\code{derive_study_day} performs study day calculation} \usage{ -calculate_study_day( +derive_study_day( sdtm_in, dm_domain, tgdt, @@ -52,6 +52,6 @@ dm <- data.frame( ) ae$AESTDTC <- as.Date(ae$AESTDTC) dm$RFSTDTC <- as.Date(dm$RFSTDTC) -calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") +derive_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") } diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-calculate_study_day.R index 599579a7..e396bbb4 100644 --- a/tests/testthat/test-calculate_study_day.R +++ b/tests/testthat/test-calculate_study_day.R @@ -9,33 +9,33 @@ dm <- data.frame( stringsAsFactors = FALSE ) -test_that("`calculate_study_day()` works as expected for invalid input", { +test_that("`derive_study_day()` works as expected for invalid input", { expect_error( - calculate_study_day("a", "b", "C", "D", "E"), + derive_study_day("a", "b", "C", "D", "E"), "sdtm_in is not a data frame" ) expect_error( - calculate_study_day(iris, "b", "C", "D", "E"), + derive_study_day(iris, "b", "C", "D", "E"), "dm_domain is not a data frame" ) expect_error( - calculate_study_day(iris, iris, "d", "e", "b"), + derive_study_day(iris, iris, "d", "e", "b"), "dm_domain needs to have the variable of refdt" ) expect_error( - calculate_study_day(iris, iris, "d", "Species"), + derive_study_day(iris, iris, "d", "Species"), "sdtm_in needs to have the variable of tgdt" ) expect_error( - calculate_study_day(iris, iris, "Petal.Length", "Species", "e"), + derive_study_day(iris, iris, "Petal.Length", "Species", "e"), "needs to have the variable of merge_key" ) expect_error( - calculate_study_day(iris, iris, "Petal.Length", "Species", 123L, "Species"), + derive_study_day(iris, iris, "Petal.Length", "Species", 123L, "Species"), "study_day_var is not a character vector" ) expect_warning( - calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AENDY"), + derive_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AENDY"), "Target date and the returned study day doesn't match." ) @@ -45,7 +45,7 @@ test_that("`calculate_study_day()` works as expected for invalid input", { stringsAsFactors = FALSE ) expect_warning( - calculate_study_day(ae, dm1, "AESTDTC", "RFSTDTC", "AESTDY"), + derive_study_day(ae, dm1, "AESTDTC", "RFSTDTC", "AESTDY"), "Reference date is not unique for each patient!" ) @@ -55,13 +55,13 @@ test_that("`calculate_study_day()` works as expected for invalid input", { stringsAsFactors = FALSE ) expect_warning( - calculate_study_day(ae, dm2, "AESTDTC", "RFSTDTC", "AESTDY"), + derive_study_day(ae, dm2, "AESTDTC", "RFSTDTC", "AESTDY"), "Encountered errors when converting refdt to dates." ) }) test_that("`calculate_study_day()` works as expected for valid input", { - res <- calculate_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") + res <- derive_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") expected <- c(-31L, 1L, NA) expect_equal(res$AESTDY, expected, tolerance = "1.5e-08") @@ -71,6 +71,6 @@ test_that("`calculate_study_day()` works as expected for valid input", { AESTDTC = c("2012-01-01", "2012-04-14", "2012-04-14"), stringsAsFactors = FALSE ) - res1 <- calculate_study_day(df, df, "AESTDTC", "RFSTDTC", "AESTDY") + res1 <- derive_study_day(df, df, "AESTDTC", "RFSTDTC", "AESTDY") expect_equal(res1$AESTDY, expected, tolerance = "1.5e-08") }) From 4379011fd83a81711c213dae5c3320d8d94d943d Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 14:55:53 +0000 Subject: [PATCH 61/66] rename files --- R/{calculate_study_day.R => derive_study_day.R} | 0 .../{test-calculate_study_day.R => test-derive_study_day.R} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename R/{calculate_study_day.R => derive_study_day.R} (100%) rename tests/testthat/{test-calculate_study_day.R => test-derive_study_day.R} (100%) diff --git a/R/calculate_study_day.R b/R/derive_study_day.R similarity index 100% rename from R/calculate_study_day.R rename to R/derive_study_day.R diff --git a/tests/testthat/test-calculate_study_day.R b/tests/testthat/test-derive_study_day.R similarity index 100% rename from tests/testthat/test-calculate_study_day.R rename to tests/testthat/test-derive_study_day.R From e6d87584979cc30d77319c2a13a817d69e0ea568 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 14:59:11 +0000 Subject: [PATCH 62/66] study day changed to integer --- R/derive_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/derive_study_day.R b/R/derive_study_day.R index bac83d5d..c0682bf9 100644 --- a/R/derive_study_day.R +++ b/R/derive_study_day.R @@ -127,6 +127,6 @@ derive_study_day <- function(sdtm_in, res <- ifelse(tgt >= ref, tgt - ref + 1L, tgt - ref) sdtm_in <- sdtm_in[original_variables] - sdtm_in[study_day_var] <- res + sdtm_in[study_day_var] <- as.integer(res) return(sdtm_in) } From 2037ba162506348e9063d8f0d6c101f6616d8181 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 15:03:47 +0000 Subject: [PATCH 63/66] update style --- R/derive_study_day.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/derive_study_day.R b/R/derive_study_day.R index c0682bf9..8444b70f 100644 --- a/R/derive_study_day.R +++ b/R/derive_study_day.R @@ -36,11 +36,11 @@ #' derive_study_day(ae, dm, "AESTDTC", "RFSTDTC", "AESTDY") #' derive_study_day <- function(sdtm_in, - dm_domain, - tgdt, - refdt, - study_day_var, - merge_key = "USUBJID") { + dm_domain, + tgdt, + refdt, + study_day_var, + merge_key = "USUBJID") { assertthat::assert_that(is.data.frame(sdtm_in)) assertthat::assert_that(is.data.frame(dm_domain)) assertthat::assert_that( From 48ba06d1dcec7cb3186de4b8c0c343b440a4d0a1 Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 4 Mar 2024 15:13:16 +0000 Subject: [PATCH 64/66] update man file --- man/derive_study_day.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/derive_study_day.Rd b/man/derive_study_day.Rd index d8352b9d..e7d4fd06 100644 --- a/man/derive_study_day.Rd +++ b/man/derive_study_day.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calculate_study_day.R +% Please edit documentation in R/derive_study_day.R \name{derive_study_day} \alias{derive_study_day} \title{\code{derive_study_day} performs study day calculation} From 2074b162c2fdbf1cd74ceaf09d511ed2fadd4e33 Mon Sep 17 00:00:00 2001 From: Rosemary Li <42564519+yli110-stat697@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:09:32 -0500 Subject: [PATCH 65/66] update doc Co-authored-by: Shiyu Chen <39443807+ShiyuC@users.noreply.github.com> --- R/derive_study_day.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/derive_study_day.R b/R/derive_study_day.R index 8444b70f..6a04c15c 100644 --- a/R/derive_study_day.R +++ b/R/derive_study_day.R @@ -14,7 +14,7 @@ #' calculate the study day. #' @param study_day_var New study day variable name in the output. For #' example, AESTDY for AE domain and CMSTDY for CM domain. -#' @param merge_key Character to represents the merging key between `sdtm_in` and +#' @param merge_key Character to represent the merging key between `sdtm_in` and #' `dm_domain`. #' #' @return Data frame that takes all columns from `sdtm_in` and a new variable From fcdcb0cb58b16f6da2879aefa54d7ba77a1e0a7f Mon Sep 17 00:00:00 2001 From: Rosemary Li Date: Mon, 11 Mar 2024 16:11:47 +0000 Subject: [PATCH 66/66] update man file --- man/derive_study_day.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/derive_study_day.Rd b/man/derive_study_day.Rd index e7d4fd06..a24eb765 100644 --- a/man/derive_study_day.Rd +++ b/man/derive_study_day.Rd @@ -27,7 +27,7 @@ calculate the study day.} \item{study_day_var}{New study day variable name in the output. For example, AESTDY for AE domain and CMSTDY for CM domain.} -\item{merge_key}{Character to represents the merging key between \code{sdtm_in} and +\item{merge_key}{Character to represent the merging key between \code{sdtm_in} and \code{dm_domain}.} } \value{