Skip to content

Conversation

@LiamHobby
Copy link
Collaborator

Closes #99

This bug occurred only when there were multiple values from a numeric type input column that were not present in the codelist i.e., values of 99 and 999 in the dataset but no appropriate record in the CT.

The fix just required placing a cli::qty() check prior to the glue substitution that uses pluralization.

This wasn't caught in testing because the test case only had a single value of wrong CT in the test dataset. I have expanded the test cases to include both single and multiple bad CT for both character and numeric input.

@LiamHobby LiamHobby requested a review from bms63 November 21, 2025 08:37
@LiamHobby LiamHobby self-assigned this Nov 21, 2025
@LiamHobby LiamHobby added 0.3.0 Issues to be worked on for release 0.3.0 (est. Jan 2026). bug Something isn't working labels Nov 21, 2025
@LiamHobby
Copy link
Collaborator Author

@bms63 Could you take a look at this one please

Liam Hobby added 2 commits December 19, 2025 14:25
Merge branch '99-create-var-from-codelist-bugfix' of github.com:pharmaverse/metatools into 99-create-var-from-codelist-bugfix
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Comment on lines +88 to +97
create_var_from_codelist(
data = data,
metacore = adlb_spec,
input_var = PARAMCD,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMCD),
decode_to_code = FALSE,
strict = TRUE
) |>
expect_warning()
Copy link
Collaborator

@bms63 bms63 Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i run this as standalone I am not getting a warning


> load(metacore::metacore_example("pilot_ADaM.rda"))
>   adlb_spec <- metacore::select_dataset(metacore, "ADLBC", quiet = TRUE)
✔ ADLBC dataset successfully selected
ℹ Dataset metadata specification subsetted with suppressed warnings

>   data <- tibble::tibble(
+     PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2")
+   )
>   compare <- tibble::tibble(
+     PARAMCD = c("ALB", "ALP", "ALT", "DUMMY", "DUMMY2"),
+     PARAM = c("Albumin (g/L)", "Alkaline Phosphatase (U/L)", "Alanine Aminotransferase (U/L)", NA, NA)
+   )
> 
>   create_var_from_codelist(
+     data = data,
+     metacore = adlb_spec,
+     input_var = PARAMCD,
+     out_var = PARAM,
+     codelist = get_control_term(adlb_spec, PARAMCD),
+     decode_to_code = FALSE,
+     strict = FALSE
+   )
# A tibble: 5 × 2
  PARAMCD PARAM                         
  <chr>   <chr>                         
1 ALB     Albumin (g/L)                 
2 ALP     Alkaline Phosphatase (U/L)    
3 ALT     Alanine Aminotransferase (U/L)
4 DUMMY   NA                            
5 DUMMY2  NA                            
> 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however the tests run fine - but im a bit suspicious

metacore = adlb_spec,
input_var = PARAMCD,
out_var = PARAM,
codelist = get_control_term(adlb_spec, PARAMCD),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
codelist = get_control_term(adlb_spec, PARAMCD),
codelist = metacore::get_control_term(adlb_spec, PARAMCD),

I thought this was from metatools at first and got confused...maybe be more explicit here with the :: for those who come later?

Comment on lines +149 to +161
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
) |>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one produced the warning when I fired off in the console

> # 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
+   )
# A tibble: 5 × 2
  PARAMN PARAM             
   <dbl> <chr>             
1     18 Sodium (mmol/L)   
2     19 Potassium (mmol/L)
3     20 Chloride (mmol/L) 
4     99 NA                
5    999 NA                
Warning message:
In `create_var_from_codelist()`: The following values present in the input dataset are not present in the
codelist: 99 and 999 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0.3.0 Issues to be worked on for release 0.3.0 (est. Jan 2026). bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_var_from_codelist throws error when strict==TRUE and input is numeric

3 participants