Skip to content
7 changes: 4 additions & 3 deletions R/codelists.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ type is {typeof(code_translation)}. Check the structure of the codelist in the \
codelist <- code_translation |> pull(ref_var)

miss <- setdiff(values, codelist)
if (strict == TRUE && length(miss) > 0) {
n_miss <- length(miss)
if (strict == TRUE && n_miss > 0) {
cli_warn(
"In {.fn create_var_from_codelist}: The following value{?s} present in the
input dataset {?is/are} not present in the codelist: {miss}"
"In {.fn create_var_from_codelist}: The following {qty(n_miss)}value{?s}
present in the input dataset {qty(n_miss)}{?is/are} not present in the codelist: {miss}"
)
}

Expand Down
36 changes: 34 additions & 2 deletions tests/testthat/test-codelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,23 @@ test_that("create_var_from_codelist", {
select(PARAMCD, PARAM) |>
expect_equal(compare)

# Test warning where arg `strict == TRUE`
# Test character variable warning where arg `strict == TRUE` / single issue case
create_var_from_codelist(
data = data,
metacore = adlb_spec,
input_var = PARAMCD,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMCD),
Comment thread
LiamHobby marked this conversation as resolved.
Outdated
decode_to_code = FALSE,
strict = TRUE
) |>
expect_warning()
Comment thread
LiamHobby marked this conversation as resolved.

# Test character variable warning where arg `strict == TRUE` / multiple issue case
data <- tibble::tibble(
PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2")
)

create_var_from_codelist(
data = data,
metacore = adlb_spec,
Expand Down Expand Up @@ -117,7 +133,7 @@ test_that("create_var_from_codelist", {
select(PARAMN, PARAM) |>
expect_equal(compare2)

# Test numeric variable used as input_var (strict == TRUE)
# Test numeric variable used as input_var / single issue case (strict == TRUE)
create_var_from_codelist(
data = data2,
metacore = adlb_spec,
Expand All @@ -129,6 +145,22 @@ test_that("create_var_from_codelist", {
) |>
expect_warning()

# Test numeric variable used as input_var / multiple issue case (strict == TRUE)
data3 <- tibble::tibble(
PARAMN = c(18, 19, 20, 99, 999)
)

create_var_from_codelist(
data = data3,
metacore = adlb_spec,
input_var = PARAMN,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMN),
decode_to_code = FALSE,
strict = TRUE
) |>
Comment thread
LiamHobby marked this conversation as resolved.
expect_warning()

# Test for Variable not in specs
expect_error(create_var_from_codelist(data, spec, VAR2, FOO))
})
Expand Down