Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# sdtm.oak V0.2.1

Cal_min_max_date (and by extension oak_cal_ref_dates) now works with raw data containing Date objects and hms time objects (not just character date and time strings).

# sdtm.oak V0.2.0

- DM Domain: New functions `calc_min_max_date()` and `oak_calc_ref_dates()` to support DM domain programming.
Expand Down
2 changes: 1 addition & 1 deletion R/cal_min_max_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ cal_min_max_date <- function(raw_dataset,

# 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]],
fin_df$datetime <- create_iso8601(as.character(raw_dataset[[date_variable]]),
.format = date_format
)
} else {
Expand Down
69 changes: 68 additions & 1 deletion tests/testthat/test-cal_min_max_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test_that("Warn if date variable parameter is NULL", {
expect_identical(observed, expected)
})

test_that("cal_min_max_date works as expected", {
test_that("cal_min_max_date works with date and time strings", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT, ~EX_ST_TM,
"001", "26-04-2022", "10:20",
Expand Down Expand Up @@ -93,3 +93,70 @@ test_that("Warning is displayed if date or time variables parameters passed are

expect_identical(observed, expected)
})

test_that("cal_min_max_date works with Date and hms (time) objects", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT, ~EX_ST_TM,
"001", as.Date("2022-04-26"), hms::hms(seconds = NULL, minutes = 20L, hours = 10L),
"001", as.Date("2022-04-25"), hms::hms(seconds = NULL, minutes = 15L, hours = 10L),
"001", as.Date("2022-04-25"), hms::hms(seconds = NULL, minutes = 19L, hours = 10L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 23L, hours = 6L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 59L, hours = 4L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 59L, hours = 5L)
)
expected_min <- tibble::tribble(
~patient_number, ~datetime,
"001", "2022-04-25T10:15:00",
"002", "2022-05-26T04:59:00"
)

expected_max <- tibble::tribble(
~patient_number, ~datetime,
"002", "2022-05-26T06:23:00",
"001", "2022-04-26T10:20:00"
)

observed_min <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "min",
date_format = "y-m-d",
time_format = "H:M:S"
)

expect_identical(observed_min, expected_min)

observed_max <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = "EX_ST_TM",
val_type = "max",
date_format = "y-m-d",
time_format = "H:M:S"
)

expect_identical(observed_max, expected_max)
})

test_that("cal_min_max_date works with Date objects (no time)", {
EX <- tibble::tribble(
~patient_number, ~EX_ST_DT,
"001", as.Date("04-25-22", "%m-%d-%y"),
"001", as.Date("04-25-22", "%m-%d-%y"),
"002", as.Date("05-26-22", "%m-%d-%y")
)
expected_min <- tibble::tribble(
~patient_number, ~datetime,
"001", create_iso8601("04-25-22", .format = "m-d-y"),
"002", create_iso8601("05-26-22", .format = "m-d-y")
)

observed_min <- cal_min_max_date(EX,
date_variable = "EX_ST_DT",
time_variable = NA,
val_type = "min",
date_format = "y-m-d",
time_format = NA
)

expect_identical(observed_min, expected_min)
})
97 changes: 97 additions & 0 deletions tests/testthat/test-oak_cal_ref_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,100 @@ test_that("Calculate the Reference dates :RFENDTC", {
expected_rfendtc <- expected |> dplyr::select(-"RFSTDTC")
expect_identical(observed_rfendtc, expected_rfendtc)
})

test_that("oak_cal_ref_dates works with Date and hms (time) objects", {
dm <- tibble::tribble(
~patient_number,
"001",
"002"
)

EX <- tibble::tribble(
~patient_number, ~EX_ST_DT, ~EX_ST_TM,
"001", as.Date("2022-04-26"), hms::hms(seconds = NULL, minutes = 20L, hours = 10L),
"001", as.Date("2022-04-25"), hms::hms(seconds = NULL, minutes = 15L, hours = 10L),
"001", as.Date("2022-04-25"), hms::hms(seconds = NULL, minutes = 19L, hours = 10L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 23L, hours = 6L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 59L, hours = 4L),
"002", as.Date("2022-05-26"), hms::hms(seconds = NULL, minutes = 59L, hours = 5L)
)

ref_date_conf_df <- tibble::tribble(
~raw_dataset_name, ~date_var, ~time_var, ~dformat, ~tformat, ~sdtm_var_name,
"EX", "EX_ST_DT", "EX_ST_TM", "y-m-d", "H:M:S", "RFSTDTC"
)

raw_source <- list(EX = EX)

observed_min <- oak_cal_ref_dates(dm,
der_var = "RFSTDTC",
min_max = "min",
ref_date_config_df = ref_date_conf_df,
raw_source = raw_source
)

expected_min <- tibble::tribble(
~patient_number, ~RFSTDTC,
"001", "2022-04-25T10:15:00",
"002", "2022-05-26T04:59:00"
)

expect_identical(observed_min, expected_min)

ref_date_conf_df_max <- tibble::tribble(
~raw_dataset_name, ~date_var, ~time_var, ~dformat, ~tformat, ~sdtm_var_name,
"EX", "EX_ST_DT", "EX_ST_TM", "y-m-d", "H:M:S", "RFENDTC"
)

observed_max <- oak_cal_ref_dates(dm,
der_var = "RFENDTC",
min_max = "max",
ref_date_config_df = ref_date_conf_df_max,
raw_source = raw_source
)

expected_max <- tibble::tribble(
~patient_number, ~RFENDTC,
"001", "2022-04-26T10:20:00",
"002", "2022-05-26T06:23:00"
)

expect_identical(observed_max, expected_max)
})

test_that("oak_cal_ref_dates works with Date objects (no time)", {
dm <- tibble::tribble(
~patient_number,
"001",
"002"
)

EX <- tibble::tribble(
~patient_number, ~EX_ST_DT,
"001", as.Date("04-25-22", "%m-%d-%y"),
"001", as.Date("04-25-22", "%m-%d-%y"),
"002", as.Date("05-26-22", "%m-%d-%y")
)

ref_date_conf_df <- tibble::tribble(
~raw_dataset_name, ~date_var, ~time_var, ~dformat, ~tformat, ~sdtm_var_name,
"EX", "EX_ST_DT", NA_character_, "y-m-d", NA_character_, "RFSTDTC"
)

raw_source <- list(EX = EX)

observed_min <- oak_cal_ref_dates(dm,
der_var = "RFSTDTC",
min_max = "min",
ref_date_config_df = ref_date_conf_df,
raw_source = raw_source
)

expected_min <- tibble::tribble(
~patient_number, ~RFSTDTC,
"001", create_iso8601("04-25-22", .format = "m-d-y"),
"002", create_iso8601("05-26-22", .format = "m-d-y")
)

expect_identical(observed_min, expected_min)
})