Skip to content

Commit

Permalink
rest of the internal funs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiyuC committed Jul 19, 2024
1 parent fd0f9dd commit 6b24f18
Show file tree
Hide file tree
Showing 59 changed files with 750 additions and 938 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export(derive_blfl)
export(derive_seq)
export(derive_study_day)
export(domain_example)
export(dtc_timepart)
export(fmt_cmp)
export(generate_oak_id_vars)
export(hardcode_ct)
Expand Down
42 changes: 1 addition & 41 deletions R/derive_blfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@
#'
#' @return Character vector containing ISO8601 dates.
#'
#' @examples
#' ## Partial or missing dates set to NA by default
#' sdtm.oak:::dtc_datepart(
#' c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00")
#' )
#' # |--> c(NA, NA, NA, NA, "2021-12-25", "2021-12-25")
#'
#' ## Prevent partial or missing dates from being set to NA
#' sdtm.oak:::dtc_datepart(
#' c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00"),
#' partial_as_na = FALSE
#' )
#' # |--> c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25")
#' @keywords internal
dtc_datepart <- function(dtc, partial_as_na = TRUE) {
# Assert that dtc is a character vector
Expand Down Expand Up @@ -51,36 +38,9 @@ dtc_datepart <- function(dtc, partial_as_na = TRUE) {
#' seconds should be ignored (default is `TRUE`).
#'
#' @return Character vector containing ISO 8601 times.
#' @export
#'
#' @keywords internal
#' ## Partial or missing times set to NA and seconds ignored by default
#' sdtm.oak:::dtc_timepart(
#' c(NA, "", "2021-12-25", "2021-12-25T12", "2021-12-25T12:30", "2021-12-25T12:30:59")
#' )
#' # |--> c(NA, NA, NA, NA, "12:30", "12:30")
#'
#' ## Prevent partial or missing times from being set to NA
#' sdtm.oak:::dtc_timepart(
#' c(NA, "", "2021-12-25", "2021-12-25T12", "2021-12-25T12:30", "2021-12-25T12:30:59"),
#' partial_as_na = FALSE
#' )
#' # |--> c(NA, "", "", "12", "12:30", "12:30")
#'
#' ## Do not ignore seconds, partial or missing times set to NA
#' sdtm.oak:::dtc_timepart(
#' c(NA, "", "2021-12-25", "2021-12-25T12", "2021-12-25T12:30", "2021-12-25T12:30:59"),
#' ignore_seconds = FALSE
#' )
#' # |--> c(NA, NA, NA, NA, NA, "12:30:59")
#'
#' ## Do not ignore seconds and prevent partial or missing times from being set to NA
#' sdtm.oak:::dtc_timepart(
#' c(NA, "", "2021-12-25", "2021-12-25T12", "2021-12-25T12:30", "2021-12-25T12:30:59"),
#' partial_as_na = FALSE,
#' ignore_seconds = FALSE
#' )
#' # |--> c(NA, "", "", "12", "12:30", "12:30:59")

dtc_timepart <- function(dtc, partial_as_na = TRUE, ignore_seconds = TRUE) {
# Assert that dtc is a character vector
admiraldev::assert_character_vector(dtc)
Expand Down
98 changes: 0 additions & 98 deletions R/dtc_create_iso8601.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ mon_abb_to_mon_num <- stats::setNames(sprintf("%02d", seq_along(month.abb)), tol
#'
#' @returns A character vector.
#'
#' @examples
#' sdtm.oak:::iso8601_na(c("10", NA_character_))
#'
#' @keywords internal
iso8601_na <- function(x) {
admiraldev::assert_character_vector(x)
Expand All @@ -30,13 +27,6 @@ iso8601_na <- function(x) {
#'
#' @returns A character vector.
#'
#' @examples
#' sdtm.oak:::zero_pad_whole_number(c(-1, 0, 1))
#'
#' sdtm.oak:::zero_pad_whole_number(c(-1, 0, 1, 10, 99, 100), n = 2)
#'
#' sdtm.oak:::zero_pad_whole_number(c(-1, 0, 1, 10, 99, 100), n = 3)
#'
#' @keywords internal
zero_pad_whole_number <- function(x, n = 2L) {
# Check `x`
Expand Down Expand Up @@ -69,18 +59,6 @@ zero_pad_whole_number <- function(x, n = 2L) {
#'
#' @returns An integer vector.
#'
#' @examples
#' sdtm.oak:::yy_to_yyyy(0:5)
#' sdtm.oak:::yy_to_yyyy(2000:2005)
#'
#' sdtm.oak:::yy_to_yyyy(90:99)
#' sdtm.oak:::yy_to_yyyy(1990:1999)
#'
#' # NB: change in behavior after 68
#' sdtm.oak:::yy_to_yyyy(65:72)
#'
#' sdtm.oak:::yy_to_yyyy(1965:1972)
#'
#' @keywords internal
yy_to_yyyy <- function(x, cutoff_2000 = 68L) {
# Check `x`
Expand All @@ -105,10 +83,6 @@ yy_to_yyyy <- function(x, cutoff_2000 = 68L) {
#'
#' @returns A character vector of the same size as `x`.
#'
#' @examples
#' x <- c("0", "00", "1", "01", "42", "100", NA_character_, "1.")
#' sdtm.oak:::iso8601_two_digits(x)
#'
#' @keywords internal
iso8601_two_digits <- function(x) {
admiraldev::assert_character_vector(x)
Expand All @@ -132,17 +106,6 @@ iso8601_min <- iso8601_two_digits
#'
#' @returns A character vector.
#'
#' @examples
#' sdtm.oak:::iso8601_year(c("0", "1", "2", "50", "68", "69", "90", "99", "00"))
#'
#' # Be default, `cutoff_2000` is at 68.
#' sdtm.oak:::iso8601_year(c("67", "68", "69", "70"))
#' sdtm.oak:::iso8601_year(c("1967", "1968", "1969", "1970"))
#'
#' # Change it to something else, e.g. `cutoff_2000 = 25`.
#' sdtm.oak:::iso8601_year(as.character(0:50), cutoff_2000 = 25)
#' sdtm.oak:::iso8601_year(as.character(1900:1950), cutoff_2000 = 25)
#'
#' @keywords internal
iso8601_year <- function(x, cutoff_2000 = 68L) {
admiraldev::assert_character_vector(x)
Expand All @@ -161,17 +124,6 @@ iso8601_year <- function(x, cutoff_2000 = 68L) {
#'
#' @returns A character vector.
#'
#' @examples
#' sdtm.oak:::iso8601_mon(c(NA, "0", "1", "2", "10", "11", "12"))
#'
#' # No semantic validation is performed on the numeric months, so `"13"` stays
#' # `"13"` but representations that can't be represented as two-digit numbers
#' # become `NA`.
#' sdtm.oak:::iso8601_mon(c("13", "99", "100", "-1"))
#'
#' (mon <- month.abb)
#' sdtm.oak:::iso8601_mon(mon)
#'
#' @keywords internal
iso8601_mon <- function(x) {
x <- tolower(x)
Expand All @@ -190,9 +142,6 @@ iso8601_mon <- function(x) {
#'
#' @returns A character vector.
#'
#' @examples
#' sdtm.oak:::iso8601_sec(c(NA, "0", "1", "10", "59", "99", "100"))
#'
#' @keywords internal
iso8601_sec <- function(x) {
x_iso8601 <- stringr::str_extract(x, "^\\d?\\d(\\.\\d*)?$")
Expand All @@ -212,36 +161,6 @@ iso8601_sec <- function(x) {
#' @param x A character vector.
#'
#' @returns A character vector.
#'
#' @examples
#' x <-
#' c(
#' "1999-01-01T15:20:01",
#' "1999-01-01T15:20:-",
#' "1999-01-01T15:-:-",
#' "1999-01-01T-:-:-",
#' "1999-01--T-:-:-",
#' "1999----T-:-:-",
#' "-----T-:-:-"
#' )
#'
#' sdtm.oak:::iso8601_truncate(x)
#'
#' # With `empty_as_na = FALSE` empty strings are not replaced with `NA`
#' sdtm.oak:::iso8601_truncate("-----T-:-:-", empty_as_na = TRUE)
#' sdtm.oak:::iso8601_truncate("-----T-:-:-", empty_as_na = FALSE)
#'
#' # Truncation only happens if missing components are the right most end,
#' # otherwise they remain unaltered.
#' sdtm.oak:::iso8601_truncate(
#' c(
#' "1999----T15:20:01",
#' "1999-01-01T-:20:01",
#' "1999-01-01T-:-:01",
#' "1999-01-01T-:-:-"
#' )
#' )
#'
#' @keywords internal
iso8601_truncate <- function(x, empty_as_na = TRUE) {
x <- stringr::str_remove(x, "[^\\d]*$")
Expand All @@ -265,23 +184,6 @@ iso8601_truncate <- function(x, empty_as_na = TRUE) {
#'
#' @returns A character vector with date-times following the ISO8601 format.
#'
#' @examples
#' cols <- c("year", "mon", "mday", "hour", "min", "sec")
#' m <- matrix(
#' c(
#' "99", "00", "01",
#' "Jan", "feb", "03",
#' "1", "01", "31",
#' "00", "12", "23",
#' "00", "59", "10",
#' "42", "5.15", NA
#' ),
#' ncol = 6,
#' dimnames = list(c(), cols)
#' )
#'
#' sdtm.oak:::format_iso8601(m)
#'
#' @keywords internal
format_iso8601 <- function(m, .cutoff_2000 = 68L) {
admiraldev::assert_integer_scalar(.cutoff_2000)
Expand Down
26 changes: 0 additions & 26 deletions R/dtc_parse_dttm.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,6 @@ parse_dttm_ <- function(dttm,
#' `"hour"`, `"min"` and `"sec"`. Each row corresponds to an element in
#' `dttm`. Each element of the matrix is the parsed date/time component.
#'
#' @examples
#' sdtm.oak:::parse_dttm("2020", "y")
#' sdtm.oak:::parse_dttm("2020-05", "y")
#'
#' sdtm.oak:::parse_dttm("2020-05", "y-m")
#' sdtm.oak:::parse_dttm("2020-05-11", "y-m-d")
#'
#' sdtm.oak:::parse_dttm("2020 05 11", "y m d")
#' sdtm.oak:::parse_dttm("2020 05 11", "y m d")
#' sdtm.oak:::parse_dttm("2020 05 11", "y\\s+m\\s+d")
#' sdtm.oak:::parse_dttm("2020 05 11", "y\\s+m\\s+d")
#'
#' sdtm.oak:::parse_dttm("2020-05-11 11:45", "y-m-d H:M")
#' sdtm.oak:::parse_dttm("2020-05-11 11:45:15.6", "y-m-d H:M:S")
#'
#' sdtm.oak:::parse_dttm(c("2002-05-11 11:45", "-05-11 11:45"), "y-m-d H:M")
#' sdtm.oak:::parse_dttm(c("2002-05-11 11:45", "-05-11 11:45"), "-m-d H:M")
#' sdtm.oak:::parse_dttm(c("2002-05-11 11:45", "-05-11 11:45"), c("y-m-d H:M", "-m-d H:M"))
#'
#' sdtm.oak:::parse_dttm("05 feb 1985 12 55 02", "d m y H M S")
#' sdtm.oak:::parse_dttm("12 55 02 05 feb 1985", "H M S d m y")
#'
#' sdtm.oak:::parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"), "y-m-d")
#' sdtm.oak:::parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"), "y-m-d", na = "UN")
#' sdtm.oak:::parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"), "y-m-d", na = c("UN", "UNK"))
#'
#' @keywords internal
parse_dttm <- function(dttm,
fmt,
Expand Down
34 changes: 0 additions & 34 deletions R/dtc_problems.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@
#' or an annotated `x`, meaning having a `problems` attribute that holds
#' parsing issues (see the Details section).
#'
#' @examples
#' date <- c("2000-01-05", "", "1980-06-18", "1979-09-07")
#' time <- c("001221", "22:35:05", "03:00:15", "07:09:00")
#' dtc <- list(date, time)
#' dttm <- c("2000-01-05", "T22:35:05", "1980-06-18T03:00:15", "1979-09-07T07:09:00")
#' is_problem <- c(TRUE, TRUE, FALSE, FALSE)
#'
#' dttm2 <- sdtm.oak:::add_problems(dttm, is_problem, dtc)
#' sdtm.oak:::problems(dttm2)
#'
#' @keywords internal
add_problems <- function(x, is_problem, dtc) {
is_x_na <- is_problem
Expand Down Expand Up @@ -89,30 +79,6 @@ add_problems <- function(x, is_problem, dtc) {
#' passed as inputs to [create_iso8601()], i.e. whose length matches the
#' number of rows of the capture matrices in `cap_matrices`.
#'
#' @examples
#' # No problem (return value is `FALSE`).
#' sdtm.oak:::any_problems(list(sdtm.oak:::parse_dttm("1980-06-18", "y-m-d")))
#'
#' # Now the parsing fails (return value is `TRUE`).
#' sdtm.oak:::any_problems(list(sdtm.oak:::parse_dttm("1980-06-18", "ymd")))
#'
#' # Find if there has been a problem in either in the `date` or `time` inputs.
#' # The following problems are expected with:
#' # - `"2001/12/25"` as it won't be parsed with the format `"y-m-d"`
#' # - `"00h12m21"` as it won't be parsed with the format `"H:M:S"`.
#' #
#' date <- c("2000-01-05", "2001/12/25", "1980-06-18", "1979-09-07")
#' time <- c("00h12m21", "22:35:05", "03:00:15", "07:09:00")
#'
#' cap_matrix_date <- sdtm.oak:::parse_dttm(date, "y-m-d")
#' cap_matrix_time <- sdtm.oak:::parse_dttm(time, "H:M:S")
#'
#' (cap_matrices <- list(cap_matrix_date, cap_matrix_time))
#'
#' # `any_problems()` returns `TRUE` for the first two elements because of the
#' # failure to parse `"2001/12/25"` and `"00h12m21"`, respectively.
#' sdtm.oak:::any_problems(cap_matrices)
#'
#' @keywords internal
any_problems <- function(cap_matrices, .cutoff_2000 = 68L) {
cap_matrices |>
Expand Down
Loading

0 comments on commit 6b24f18

Please sign in to comment.