Skip to content

Commit

Permalink
Optimize calculation of combination names
Browse files Browse the repository at this point in the history
Makes calling extractCombinationsImpl about 3x faster
  • Loading branch information
halhen committed Jul 10, 2022
1 parent 12f306f commit 1ff010a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions R/data-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,23 @@ extractCombinationsImpl <- function(df,
cc <- colorLookup(colors)

elems <- rownames(df)
dfSets <- df[, allSetNames]
cName <- apply(dfSets, 1, function(r) {
nn <- allSetNames[as.logical(r)]
if (length(nn) == 1) {
nn
} else {
paste(nn, collapse = symbol)
}

# Calculate combinations names.
# First, translate from 1/0 per set for member or not into the name of each
# set + separating symbol if member, or empty string if not.
translatedSets <- lapply(allSetNames, function(setName) {
# Same as ifelse(df[[setName]] == 1, paste0(setName, symbol), ""), but a lot faster
c("", paste0(setName, symbol))[df[[setName]] + 1]
})
# Then paste0() these translated names by row
cName <- do.call(paste0, translatedSets)

dd <- aggregate(elems, list(c_name = cName), function(r) {
r
})
setNames <- strsplit(dd$c_name, symbol, fixed = TRUE)
# We got an extra symbol with the translatedSets above; clean it up
dd$c_name <- vapply(setNames, paste0, collapse = symbol, character(1))
setColors <- cc(dd$c_name)

combinations <- lapply(seq_len(nrow(dd)), function(i) {
Expand Down

0 comments on commit 1ff010a

Please sign in to comment.