Skip to content

Commit 7755619

Browse files
committed
Add argument "makeUnique"
1 parent 639ef17 commit 7755619

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

R/createHashFromColumns.R

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# createHashFromColumns --------------------------------------------------------
2-
createHashFromColumns <- function(data, columns, nchars = 8L, silent = FALSE)
2+
createHashFromColumns <- function(
3+
data, columns, nchars = 8L, silent = FALSE, makeUnique = FALSE
4+
)
35
{
46
duplicates <- kwb.utils::findPartialDuplicates(data, columns)
57

68
if (!is.null(duplicates)) {
7-
89
if (!silent) {
910
message(
1011
"Cannot create unique hashes due to duplicates in the key columns (",
1112
kwb.utils::stringList(columns),
12-
")! Returning -1L. Check attribute 'duplicates'."
13+
")! "
1314
)
15+
if (makeUnique) {
16+
message("I will make the hashes unique.")
17+
} else {
18+
message("Returning -1L. Check attribute 'duplicates'.")
19+
}
20+
}
21+
if (!makeUnique) {
22+
return(structure(-1L, duplicates = duplicates))
1423
}
15-
16-
return(structure(-1L, duplicates = duplicates))
1724
}
1825

1926
keys <- kwb.utils::pasteColumns(data, columns, "|")
20-
21-
stopifnot(!anyDuplicated(keys))
27+
28+
if (!makeUnique) {
29+
stopifnot(!anyDuplicated(keys))
30+
}
2231

2332
hashes <- kwb.utils::left(unlist(lapply(keys, digest::digest)), nchars)
2433

25-
stopifnot(!anyDuplicated(hashes))
34+
if (makeUnique) {
35+
hashes <- kwb.utils::makeUnique(hashes, warn = FALSE)
36+
}
2637

38+
stopifnot(!anyDuplicated(hashes))
39+
2740
hashes
2841
}

0 commit comments

Comments
 (0)