From 1ff010acea936a58a50530da62dee582b6407d05 Mon Sep 17 00:00:00 2001 From: Henrik Lindberg Date: Sun, 10 Jul 2022 10:08:45 +0200 Subject: [PATCH] Optimize calculation of combination names Makes calling extractCombinationsImpl about 3x faster --- R/data-helpers.R | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/R/data-helpers.R b/R/data-helpers.R index c5a260c..af759d8 100644 --- a/R/data-helpers.R +++ b/R/data-helpers.R @@ -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) {