From 56e1b9c745e0cba7079afff81e82408fcbe49052 Mon Sep 17 00:00:00 2001 From: EhrmannS Date: Thu, 14 Dec 2023 13:35:27 +0100 Subject: [PATCH] fix a bug where external concepts would not be mapped if they already appear in the external table. new mappings should also be possible, even if some other mapping had already been made. --- R/new_mapping.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/R/new_mapping.R b/R/new_mapping.R index 6368616..60cffb4 100644 --- a/R/new_mapping.R +++ b/R/new_mapping.R @@ -73,8 +73,7 @@ new_mapping <- function(new = NULL, target, source = NULL, lut = NULL, match = NULL, certainty = NULL, type = "concept", - ontology = NULL, verbose = FALSE, - beep = NULL){ + ontology = NULL, verbose = FALSE, beep = NULL){ assertCharacter(x = new, all.missing = FALSE) assertDataFrame(x = target, nrows = length(new)) @@ -197,22 +196,23 @@ new_mapping <- function(new = NULL, target, source = NULL, lut = NULL, # } - # identify concepts that are not yet in the external concepts extMps <- temp %>% distinct(new, has_broader, has_source) %>% filter(new != "") %>% rename(label = new) + # identify external mappings that are not yet in the external table if("has_broader" %in% names(theTable$external)){ - extMps <- extMps %>% + newExtMps <- extMps %>% anti_join(theTable$external, by = c("label", "has_source", "has_broader")) } else { - extMps <- extMps %>% + newExtMps <- extMps %>% anti_join(theTable$external, by = c("label", "has_source")) } - if(!is.null(lut) & !dim(extMps)[1] == 0){ - extMps <- extMps %>% + # make a new ID for the new external concepts + if(!is.null(lut) & !dim(newExtMps)[1] == 0){ + newExtMps <- newExtMps %>% distinct(label, has_source) %>% right_join(lut, by = "label") %>% mutate(newid = paste0(source, "_", row_number() + prevID), @@ -220,13 +220,13 @@ new_mapping <- function(new = NULL, target, source = NULL, lut = NULL, has_source = srcID) %>% select(id = newid, label, has_broader, has_source, description) } else { - extMps <- extMps %>% + newExtMps <- newExtMps %>% mutate(newid = paste0(source, "_", row_number() + prevID)) %>% select(id = newid, label, has_broader, has_source) %>% mutate(description = NA_character_) } - theTable$external <- extMps %>% + theTable$external <- newExtMps %>% bind_rows(theTable$external, .) # map external concept to harmonised table