Skip to content

Commit 7dd59ab

Browse files
authored
Merge pull request #102 from pharmaverse/99-create-var-from-codelist-bugfix
99 create var from codelist bugfix
2 parents e550f2d + 08f1782 commit 7dd59ab

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

R/codelists.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,11 @@ type is {typeof(code_translation)}. Check the structure of the codelist in the \
191191
codelist <- code_translation |> pull(ref_var)
192192

193193
miss <- setdiff(values, codelist)
194-
if (strict == TRUE && length(miss) > 0) {
194+
n_miss <- length(miss)
195+
if (strict == TRUE && n_miss > 0) {
195196
cli_warn(
196-
"In {.fn create_var_from_codelist}: The following value{?s} present in the
197-
input dataset {?is/are} not present in the codelist: {miss}"
197+
"In {.fn create_var_from_codelist}: The following {qty(n_miss)}value{?s}
198+
present in the input dataset {qty(n_miss)}{?is/are} not present in the codelist: {miss}"
198199
)
199200
}
200201

tests/testthat/test-codelist.R

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,23 @@ test_that("create_var_from_codelist", {
8484
select(PARAMCD, PARAM) |>
8585
expect_equal(compare)
8686

87-
# Test warning where arg `strict == TRUE`
87+
# Test character variable warning where arg `strict == TRUE` / single issue case
88+
create_var_from_codelist(
89+
data = data,
90+
metacore = adlb_spec,
91+
input_var = PARAMCD,
92+
out_var = PARAM,
93+
codelist = metacore::get_control_term(adlb_spec, PARAMCD),
94+
decode_to_code = FALSE,
95+
strict = TRUE
96+
) |>
97+
expect_warning()
98+
99+
# Test character variable warning where arg `strict == TRUE` / multiple issue case
100+
data <- tibble::tibble(
101+
PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2")
102+
)
103+
88104
create_var_from_codelist(
89105
data = data,
90106
metacore = adlb_spec,
@@ -117,7 +133,7 @@ test_that("create_var_from_codelist", {
117133
select(PARAMN, PARAM) |>
118134
expect_equal(compare2)
119135

120-
# Test numeric variable used as input_var (strict == TRUE)
136+
# Test numeric variable used as input_var / single issue case (strict == TRUE)
121137
create_var_from_codelist(
122138
data = data2,
123139
metacore = adlb_spec,
@@ -129,8 +145,27 @@ test_that("create_var_from_codelist", {
129145
) |>
130146
expect_warning()
131147

148+
# Test numeric variable used as input_var / multiple issue case (strict == TRUE)
149+
data3 <- tibble::tibble(
150+
PARAMN = c(18, 19, 20, 99, 999)
151+
)
152+
153+
create_var_from_codelist(
154+
data = data3,
155+
metacore = adlb_spec,
156+
input_var = PARAMN,
157+
out_var = PARAM,
158+
codelist = get_control_term(adlb_spec, PARAMN),
159+
decode_to_code = FALSE,
160+
strict = TRUE
161+
) |>
162+
expect_warning()
163+
132164
# Test for Variable not in specs
133-
expect_error(create_var_from_codelist(data, spec, VAR2, FOO))
165+
expect_error(
166+
create_var_from_codelist(data, adlb_spec, VAR2, FOO),
167+
regexp = cli_inform(c("!" = "FOO not found in the value_spec table. Please check the variable name"))
168+
)
134169
})
135170

136171
test_that("create_cat_var", {

0 commit comments

Comments
 (0)