Skip to content

Commit

Permalink
Add argument "makeUnique"
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Nov 15, 2024
1 parent 639ef17 commit 7755619
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions R/createHashFromColumns.R
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7755619

Please sign in to comment.