From 7755619596a8b9584afeea07b28595c25be0ddfe Mon Sep 17 00:00:00 2001 From: hsonne Date: Fri, 15 Nov 2024 21:53:55 +0100 Subject: [PATCH] Add argument "makeUnique" --- R/createHashFromColumns.R | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/R/createHashFromColumns.R b/R/createHashFromColumns.R index a62572c..56abe7a 100644 --- a/R/createHashFromColumns.R +++ b/R/createHashFromColumns.R @@ -1,28 +1,41 @@ # createHashFromColumns -------------------------------------------------------- -createHashFromColumns <- function(data, columns, nchars = 8L, silent = FALSE) +createHashFromColumns <- function( + data, columns, nchars = 8L, silent = FALSE, makeUnique = FALSE +) { duplicates <- kwb.utils::findPartialDuplicates(data, columns) if (!is.null(duplicates)) { - if (!silent) { message( "Cannot create unique hashes due to duplicates in the key columns (", kwb.utils::stringList(columns), - ")! Returning -1L. Check attribute 'duplicates'." + ")! " ) + if (makeUnique) { + message("I will make the hashes unique.") + } else { + message("Returning -1L. Check attribute 'duplicates'.") + } + } + if (!makeUnique) { + return(structure(-1L, duplicates = duplicates)) } - - return(structure(-1L, duplicates = duplicates)) } keys <- kwb.utils::pasteColumns(data, columns, "|") - - stopifnot(!anyDuplicated(keys)) + + if (!makeUnique) { + stopifnot(!anyDuplicated(keys)) + } hashes <- kwb.utils::left(unlist(lapply(keys, digest::digest)), nchars) - stopifnot(!anyDuplicated(hashes)) + if (makeUnique) { + hashes <- kwb.utils::makeUnique(hashes, warn = FALSE) + } + stopifnot(!anyDuplicated(hashes)) + hashes }