diff --git a/DESCRIPTION b/DESCRIPTION index 866453d..fd69555 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: CodelistGenerator Title: Identify Relevant Clinical Codes and Evaluate Their Use -Version: 3.2.0 +Version: 3.2.0.900 Authors@R: c( person("Edward", "Burn", email = "edward.burn@ndorms.ox.ac.uk", role = c("aut", "cre"), diff --git a/R/drugCodes.R b/R/drugCodes.R index fb44a5e..69c359a 100644 --- a/R/drugCodes.R +++ b/R/drugCodes.R @@ -191,6 +191,8 @@ getATCCodes <- function(cdm, #' @param name Names of ingredients of interest. For example, c("acetaminophen", #' "codeine"), would result in a list of length two with the descendant #' concepts for these two particular drug ingredients. +#' @param nameStyle Name style to apply to returned list. Can be one of +#' "{concept_code}_{concept_name}", "{concept_code}", or "{concept_name}". #' @param doseForm Only descendants codes with the specified dose form #' will be returned. If NULL, descendant codes will be returned regardless #' of dose form. @@ -214,11 +216,13 @@ getATCCodes <- function(cdm, #' @examples #' \dontrun{ #' cdm <- mockVocabRef() -#' getDrugIngredientCodes(cdm = cdm, name = "Adalimumab") +#' getDrugIngredientCodes(cdm = cdm, name = "Adalimumab", +#' nameStyle = "{concept_name}") #' CDMConnector::cdmDisconnect(cdm) #'} getDrugIngredientCodes <- function(cdm, name = NULL, + nameStyle = "{concept_code}_{concept_name}", doseForm = NULL, doseUnit = NULL, routeCategory = NULL, @@ -236,6 +240,10 @@ getDrugIngredientCodes <- function(cdm, add = errorMessage, null.ok = TRUE ) + checkmate::assert_choice(x = nameStyle, + choices = c("{concept_code}_{concept_name}", + "{concept_code}", + "{concept_name}")) checkmate::assertCharacter(type, len = 1) checkmate::reportAssertions(collection = errorMessage) @@ -303,14 +311,13 @@ getDrugIngredientCodes <- function(cdm, f = as.factor(ingredientCodes$ancestor_concept_id), drop = TRUE ) - names(ingredientCodes) <- dplyr::tibble(concept_id = names(ingredientCodes)) |> dplyr::mutate(seq = dplyr::row_number()) |> dplyr::left_join(ingredientConcepts |> dplyr::mutate(concept_id = as.character(.data$concept_id)), by= "concept_id") |> - dplyr::mutate(new_name = paste0(.data$concept_code, "_", - omopgenerics::toSnakeCase(.data$concept_name))) |> + dplyr::mutate(concept_name = paste0(omopgenerics::toSnakeCase(.data$concept_name)), + new_name = glue::glue(nameStyle)) |> dplyr::arrange(seq) |> dplyr::pull("new_name") diff --git a/man/getDrugIngredientCodes.Rd b/man/getDrugIngredientCodes.Rd index ac9f226..78ce64a 100644 --- a/man/getDrugIngredientCodes.Rd +++ b/man/getDrugIngredientCodes.Rd @@ -7,6 +7,7 @@ getDrugIngredientCodes( cdm, name = NULL, + nameStyle = "{concept_code}_{concept_name}", doseForm = NULL, doseUnit = NULL, routeCategory = NULL, @@ -21,6 +22,9 @@ getDrugIngredientCodes( "codeine"), would result in a list of length two with the descendant concepts for these two particular drug ingredients.} +\item{nameStyle}{Name style to apply to returned list. Can be one of +"{concept_code}_{concept_name}", "{concept_code}", or "{concept_name}".} + \item{doseForm}{Only descendants codes with the specified dose form will be returned. If NULL, descendant codes will be returned regardless of dose form.} @@ -51,7 +55,8 @@ Get descendant codes for drug ingredients \examples{ \dontrun{ cdm <- mockVocabRef() -getDrugIngredientCodes(cdm = cdm, name = "Adalimumab") +getDrugIngredientCodes(cdm = cdm, name = "Adalimumab", + nameStyle = "{concept_name}") CDMConnector::cdmDisconnect(cdm) } } diff --git a/tests/testthat/test-dbms.R b/tests/testthat/test-dbms.R index 1df9713..259ce6d 100644 --- a/tests/testthat/test-dbms.R +++ b/tests/testthat/test-dbms.R @@ -49,8 +49,22 @@ test_that("redshift", { expect_true(nrow(asthma) > 0) # drug ingredients - expect_no_error(metformin <- getDrugIngredientCodes(cdm, "metformin")) + expect_no_error(metformin <- getDrugIngredientCodes(cdm, "metformin", + nameStyle = "{concept_name}")) expect_true(inherits(metformin, "codelist")) + expect_true("metformin" %in% names(metformin)) + + expect_no_error(metformin_2 <- getDrugIngredientCodes(cdm, "metformin", + nameStyle = "{concept_code}")) + expect_true("6809" %in% names(metformin_2)) + + expect_no_error(metformin_3 <- getDrugIngredientCodes(cdm, "metformin", + nameStyle = "{concept_code}_{concept_name}")) + expect_true("6809_metformin" %in% names(metformin_3)) + + expect_error(getDrugIngredientCodes(cdm, "metformin", + nameStyle = "something else")) + # achilles cdm$achilles_results <- cdm$condition_occurrence %>% diff --git a/tests/testthat/test-drugCodes.R b/tests/testthat/test-drugCodes.R index 2d31184..87ace0e 100644 --- a/tests/testthat/test-drugCodes.R +++ b/tests/testthat/test-drugCodes.R @@ -395,6 +395,14 @@ test_that("no duplicate names example 1",{ overwrite = TRUE) attr(cdm, "write_schema") <- "main" + expect_error(getDrugIngredientCodes( + cdm = cdm, name = "Adalimumab", nameStyle = "{concept_name}" + )) + + expect_no_error(getDrugIngredientCodes( + cdm = cdm, name = "Adalimumab", nameStyle = "{concept_code}" + )) + ingredient_list <- getDrugIngredientCodes( cdm = cdm, name = "Adalimumab" )