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") })