Skip to content

Commit

Permalink
Updates to address the review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzama-1990 committed Nov 5, 2024
1 parent 40dc132 commit 1d5bcb1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 29 deletions.
27 changes: 15 additions & 12 deletions R/cal_min_max_date.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Calculate minimum and maximum date and time in the dataframe
#' Calculate minimum and maximum date and time in the data frame
#'
#' @description This function derives the earliest/latest ISO8601 datetime
#'
Expand All @@ -18,24 +18,24 @@
#' @examples
#' EX <- tibble::tribble(
#' ~patient_number, ~EX_ST_DT, ~EX_ST_TM,
#' "001", "26-04-2022", "10:20",
#' "001", "25-04-2022", "10:20",
#' "001", "25-04-2022", "10:15",
#' "001", "25-04-2022", "10:19",
#' "002", "26-05-2022", "UNK:UNK",
#' "002", "26-05-2022", "05:59"
#' )
#'
#' min <- cal_min_max_date(EX,
#' "EX_ST_DT",
#' "EX_ST_TM",
#' date_variable = "EX_ST_DT",
#' time_variable = "EX_ST_TM",
#' val_type = "min",
#' date_format = "dd-mmm-yyyy",
#' time_format = "H:M"
#' )
#'
#' max <- cal_min_max_date(EX,
#' "EX_ST_DT",
#' "EX_ST_TM",
#' date_variable = "EX_ST_DT",
#' time_variable = "EX_ST_TM",
#' val_type = "max",
#' date_format = "dd-mmm-yyyy",
#' time_format = "H:M"
Expand All @@ -47,13 +47,15 @@ cal_min_max_date <- function(raw_dataset,
val_type = "min",
date_format,
time_format) {
# Check if date is present in the raw data frame
date_not_in_data <- !(date_variable %in% colnames(raw_dataset))
# Check if date parameter is missing or date variable is present in the raw data frame
date_not_in_data <- is.na(date_variable) ||
!utils::hasName(raw_dataset, date_variable)

# Check if time variable is used and present in the raw data frame
time_not_in_data <- !(time_variable %in% colnames(raw_dataset)) && !is.na(time_variable)
time_not_in_data <- !is.na(time_variable) &&
!utils::hasName(raw_dataset, time_variable)

# If date/time variables not present return the empty data frame
# If date/time variables not present return empty data frame
if (date_not_in_data || time_not_in_data) {
# Return Empty data frame with patient_number and datetime columns
empty_df <- stats::setNames(
Expand All @@ -68,13 +70,14 @@ cal_min_max_date <- function(raw_dataset,
}

fin_df <- raw_dataset
# Time variable is not used then use only date

# Time is not used in reference date then use only date
if (is.na(time_variable)) {
fin_df$datetime <- create_iso8601(raw_dataset[[date_variable]],
.format = date_format
)
} else {
# If both date and time variables are presen use both date and time
# If both date and time variables are present use both date and time
raw_dataset$date_time <- paste0(
raw_dataset[[date_variable]],
raw_dataset[[time_variable]]
Expand Down
13 changes: 10 additions & 3 deletions R/oak_cal_ref_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' Default set to Minimum. Values should be min or max.
#' @param ref_date_config_df Data frame which has the details of the variables to
#' be used for the calculation of reference dates.
#' Should has columns listed below:
#' Should have columns listed below:
#' dataset_name : Name of the raw dataset.
#' date_var : Date variable name from the raw dataset.
#' time_var : Time variable name from the raw dataset.
Expand Down Expand Up @@ -59,7 +59,7 @@
#'
#' dm_df <- oak_cal_ref_dates(dm,
#' der_var = "RFSTDTC",
#' min_max = "max",
#' min_max = "min",
#' ref_date_config_df = ref_date_config_df,
#' raw_source
#' )
Expand All @@ -76,6 +76,8 @@ oak_cal_ref_dates <- function(ds_in = dm,
tformat, sdtm_var_name
))

admiraldev::assert_list_of(raw_source, "data.frame")

ds_out <- data.frame()
for (i in seq_along(ref_date_config_df$dataset_name)) {
raw_dataset_name <- ref_date_config_df$dataset_name[i]
Expand All @@ -86,7 +88,7 @@ oak_cal_ref_dates <- function(ds_in = dm,
sdtm_var <- ref_date_config_df$sdtm_var_name[i]
raw_dataset <- raw_source[[raw_dataset_name]]

if (der_var == sdtm_var) {
if (der_var == sdtm_var && !is.null(raw_dataset)) {
ds_out1 <- cal_min_max_date(
raw_dataset = raw_dataset,
date_variable = date_variable,
Expand All @@ -96,6 +98,11 @@ oak_cal_ref_dates <- function(ds_in = dm,
val_type = min_max
)
ds_out <- rbind(ds_out, ds_out1)
} else if (der_var == sdtm_var && is.null(raw_dataset)) {
warning(paste0(
raw_dataset_name,
" is not present in the source data list but referenced in ref_date_config_df"
))
}
}

Expand Down
12 changes: 6 additions & 6 deletions man/cal_min_max_date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/oak_cal_ref_dates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 32 additions & 6 deletions tests/testthat/test-cal_min_max_date.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
test_that("Warn if date variable parameter is NULL", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT,
"001", "26-04-2022"
)

warning_msg <- "Date variable NA or Time variable NA not present in source data"
expect_warning(
observed <- cal_min_max_date(EX,
date_variable = NA,
time_variable = NA,
val_type = "max",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
),
regexp = warning_msg
)

expected <- stats::setNames(
data.frame(matrix(ncol = 2L, nrow = 0L)),
c("patient_number", "datetime")
)

expect_identical(observed, expected)
})

test_that("cal_min_max_date works as expected", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT, ~EX_ST_TM,
Expand All @@ -22,8 +48,8 @@ test_that("cal_min_max_date works as expected", {
)

observed_min <- cal_min_max_date(EX,
"EX_ST_DT",
"EX_ST_TM",
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "min",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
Expand All @@ -32,8 +58,8 @@ test_that("cal_min_max_date works as expected", {
expect_identical(observed_min, expected_min)

observed_max <- cal_min_max_date(EX,
"EX_ST_DT",
"EX_ST_TM",
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "max",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
Expand All @@ -51,8 +77,8 @@ test_that("Warning is displayed if date or time variables parameters passed are
warning_msg <- "Date variable EX_ST_DT or Time variable EX_ST_TM not present in source data"
expect_warning(
observed <- cal_min_max_date(EX,
"EX_ST_DT",
"EX_ST_TM",
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "max",
date_format = "dd-mmm-yyyy",
time_format = "H:M"
Expand Down

0 comments on commit 1d5bcb1

Please sign in to comment.