Skip to content

Commit

Permalink
refactor: move function
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Feb 13, 2021
1 parent fc2b5fd commit 907c915
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 72 deletions.
37 changes: 0 additions & 37 deletions r_package/R/data-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,3 @@ extractCombinationsImpl = function(df,
names(combinations) = NULL
sortSets(combinations, order.by, limit)
}

generateCombinations = function(upsetjs,
c_type,
min,
max,
empty,
order.by,
limit,
colors = NULL,
symbol = '&') {
checkUpSetArgument(upsetjs)
stopifnot(is.numeric(min), length(min) == 1)
stopifnottype('max', max)
stopifnot(is.logical(empty), length(empty) == 1)
stopifnot(is.character(order.by), length(order.by) >= 1)
stopifnot(is.null(limit) ||
(is.numeric(limit) && length(limit) == 1))
stopifnot(is.null(colors) || is.list(colors))
stopifnot(c_type == "intersection" ||
c_type == "union" || c_type == "distinctIntersection")

if (inherits(upsetjs, 'upsetjs_common')) {
sets = upsetjs$x$sets
gen = generateCombinationsImpl(sets, c_type, min, max, empty, order.by, limit, colors, symbol)
} else {
# proxy
gen = cleanNull(list(
type = c_type,
min = min,
max = max,
empty = empty,
order = order.by,
limit = limit
))
}
setProperty(upsetjs, 'combinations', gen)
}
76 changes: 41 additions & 35 deletions r_package/R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,7 @@ fromList = function(upsetjs,
cc = colorLookup(colors)
toSet = function(key, value) {
elems <<- unique(c(elems, value))
structure(list(
name = key,
type = 'set',
elems = value,
cardinality = length(value),
color = cc(key)
),
class = "upsetjs_set")
asSet(key, value, color=cc(key))
}
sets = mapply(toSet,
key = names(value),
Expand Down Expand Up @@ -194,17 +187,7 @@ fromExpression = function(upsetjs,
raw_combinations = value

toCombination = function(key, value, color) {
structure(
list(
name = key,
type = type,
elems = c(),
color = cc(key),
cardinality = value,
setNames = unlist(strsplit(key, symbol))
),
class = "upsetjs_combination"
)
asCombination(key, c(), type, sets = unlist(strsplit(key, symbol)), cardinality = value, color=cc(key))
}
combinations = mapply(
toCombination,
Expand All @@ -221,14 +204,7 @@ fromExpression = function(upsetjs,
for (s in c$setNames) {
if (!(s %in% defined_sets)) {
defined_sets = c(defined_sets, s)
sets[[s]] = structure(list(
name = s,
type = "set",
color = cc(s),
elems = c(),
cardinality = 0
),
class = "upsetjs_set")
sets[[s]] = asSet(s, c(), color=cc(s))
}
# determine base set based on type and value
set = sets[[s]]
Expand Down Expand Up @@ -288,14 +264,7 @@ extractSetsFromDataFrame = function(df,
elems = rownames(df)
toSet = function(key) {
sub = elems[df[[key]] == TRUE]
structure(list(
name = key,
type = "set",
color = cc(key),
cardinality = length(sub),
elems = if(store.elems) { sub } else { c() }
),
class = "upsetjs_set")
asSet(key, ifelse(store.elems, sub, c()), cardinality=length(sub), color=cc(key))
}

set_names = setdiff(colnames(df), if (is.character(attributes))
Expand Down Expand Up @@ -514,6 +483,43 @@ setCombinations = function(upsetjs, value) {
setProperty(upsetjs, 'combinations', value)
}

generateCombinations = function(upsetjs,
c_type,
min,
max,
empty,
order.by,
limit,
colors = NULL,
symbol = '&') {
checkUpSetArgument(upsetjs)
stopifnot(is.numeric(min), length(min) == 1)
stopifnottype('max', max)
stopifnot(is.logical(empty), length(empty) == 1)
stopifnot(is.character(order.by), length(order.by) >= 1)
stopifnot(is.null(limit) ||
(is.numeric(limit) && length(limit) == 1))
stopifnot(is.null(colors) || is.list(colors))
stopifnot(c_type == "intersection" ||
c_type == "union" || c_type == "distinctIntersection")

if (inherits(upsetjs, 'upsetjs_common')) {
sets = upsetjs$x$sets
gen = generateCombinationsImpl(sets, c_type, min, max, empty, order.by, limit, colors, symbol)
} else {
# proxy
gen = cleanNull(list(
type = c_type,
min = min,
max = max,
empty = empty,
order = order.by,
limit = limit
))
}
setProperty(upsetjs, 'combinations', gen)
}

#'
#' configure the generation of the intersections
#' @param upsetjs an object of class \code{upsetjs} or \code{upsetjs_proxy}
Expand Down

0 comments on commit 907c915

Please sign in to comment.