diff --git a/R/updateColumnValues.R b/R/updateColumnValues.R new file mode 100644 index 0000000..01486b4 --- /dev/null +++ b/R/updateColumnValues.R @@ -0,0 +1,27 @@ +#' Update values in a summarised result +#' +#' @description +#' Updates the character values of specific column(s) in a summarised result object +#' (e.g., changing cohort names to a more polished version for Shiny app labels) +#' according to a given name mapping. +#' +#' @param summarised_result The summarised result with values to update. +#' @param names_map A named vector containing the mapping between old and new names. +#' @param variable The column name(s) containing the values to be updated in the +#' summarised result object. +#' +#' @returns The summarised result itself with updated values. +#' +updateColumnValues <- function( + summarised_result, + names_map, + variable +) { + + summarised_result[[variable]] <- dplyr::recode( + summarised_result[[variable]], + !!!names_map + ) + + return(summarised_result) +} \ No newline at end of file diff --git a/man/updateColumnValues.Rd b/man/updateColumnValues.Rd new file mode 100644 index 0000000..5c5a237 --- /dev/null +++ b/man/updateColumnValues.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/updateColumnValues.R +\name{updateColumnValues} +\alias{updateColumnValues} +\title{Update values in a summarised result} +\usage{ +updateColumnValues(summarised_result, names_map, variable) +} +\arguments{ +\item{summarised_result}{The summarised result with values to update.} + +\item{names_map}{A named vector containing the mapping between old and new names.} + +\item{variable}{The column name(s) containing the values to be updated in the +summarised result object.} +} +\value{ +The summarised result itself with updated values. +} +\description{ +Updates the character values of specific column(s) in a summarised result object +(e.g., changing cohort names to a more polished version for Shiny app labels) +according to a given name mapping. +} diff --git a/tests/testthat/test-cdmNames.R b/tests/testthat/test-cdmNames.R index 17f44ba..2c9c6f8 100644 --- a/tests/testthat/test-cdmNames.R +++ b/tests/testthat/test-cdmNames.R @@ -1,5 +1,5 @@ test_that("assertCdmNames works", { - # Assert a group of acryonims + # Assert a group of acronyms labels <- c( "BCR", "IQVIA LPD Belgium", @@ -9,9 +9,9 @@ test_that("assertCdmNames works", { ) assertCdmNames(labels = labels) |> expect_invisible() - # Error acronim do not match + # Error acronym do not match labels <- c( - "BCRX", # Mispelled acronym + "BCRX", # Misspelled acronym "IQVIA LPD Belgium", "NLHR@UiO:PERINATAL", "IQVIA US - AmbEMR", @@ -22,7 +22,7 @@ test_that("assertCdmNames works", { }) test_that("assertCdmNames against expected acronyms", { - # Assert a group of acryonims + # Assert a group of acronyms labels <- c( "BCR", "IQVIA LPD Belgium", @@ -42,7 +42,7 @@ test_that("assertCdmNames against expected acronyms", { expect_error() # Incorrect acronym labels <- c( - "BCRX", # Mispelled acronym + "BCRX", # Misspelled acronym "IQVIA LPD Belgium", "NLHR@UiO:PERINATAL", "IQVIA US - AmbEMR", @@ -78,9 +78,9 @@ test_that("arrangeCdmNames returns required acronyms in order", { "IQVIA US - AmbEMR", "IQVIA US - PMTX+" )) - # Error acronim do not match + # Error acronym do not match labels <- c( - "BCRX", # Mispelled acronym + "BCRX", # Misspelled acronym "IQVIA LPD Belgium", "NLHR@UiO:PERINATAL", "IQVIA US - AmbEMR", diff --git a/tests/testthat/test-updateColumnValues.R b/tests/testthat/test-updateColumnValues.R new file mode 100644 index 0000000..5e5c1dd --- /dev/null +++ b/tests/testthat/test-updateColumnValues.R @@ -0,0 +1,48 @@ +test_that("updateColumnValues updates summarised result correctly", { + + # Create mock summarised result + x <- dplyr::tibble( + "result_id" = 1L, + "cdm_name" = "cprd", + "group_name" = "cohort_name", + "group_level" = c( + "acetaminophen", + "acetaminophen", + "diclofenac", + "ibuprofen" + ), + "strata_name" = "sex &&& age_group", + "strata_level" = c( + "male &&& <40", + "male &&& >=40", + "male &&& >=40", + "male &&& >=40" + ), + "variable_name" = "number_subjects", + "variable_level" = NA_character_, + "estimate_name" = "count", + "estimate_type" = "integer", + "estimate_value" = c("5", "15", "8", "12"), + "additional_name" = "overall", + "additional_level" = "overall" + ) |> + omopgenerics::newSummarisedResult() + + # Define mapping to custom new names + names_map <- c( + "acetaminophen" = "Acetamoniphen cohort", + "diclofenac" ="Diclofenac cohort (outdated)", + "ibuprofen" = "Ibuprofen cohort" + ) + + # Test function + x <- updateColumnValues( + summarised_result = x, + names_map = names_map, + variable = "group_level" + ) + + check_names <- all(x$group_level %in% unname(names_map)) + expect_true(check_names) + +})