Skip to content

Commit b60118c

Browse files
authored
fix: fixed an error when adding alternatives (#9)
* Fixed an error when adding alternatives whilst some empty condition blocks were present would cause an error message in the UI. * Also refactored `or_filtering_transformator` a bunch. Should be now more readable.
1 parent 7fa6e76 commit b60118c

12 files changed

Lines changed: 394 additions & 186 deletions

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RoxygenNote: 7.3.3
2525
Depends:
2626
R (>= 4.1.0)
2727
Imports:
28+
R6,
2829
checkmate,
2930
cowplot,
3031
dplyr,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export(or_filtering_transformator)
1111
export(patchwork_plot_decorator)
1212
export(remove_by_label)
1313
export(title_footer_decorator)
14+
import(R6)
1415
import(checkmate)
1516
import(dplyr)
1617
import(ggplot2)

R/BlockConditions.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#' Class `BlockConditions`
2+
#'
3+
#' This class represents a collection of conditions used for filtering datasets.
4+
#'
5+
#' @section Slots:
6+
#' \describe{
7+
#' \item{\code{conditions}}{A list of conditions,
8+
#' where each condition is a list containing variable, operator, and value.}
9+
#' }
10+
#' @keywords internal
11+
setClass("BlockConditions", slots = list(conditions = "list"))
12+
13+
#' Add a condition to a `BlockConditions` object
14+
#'
15+
#' @param object A `BlockConditions` object.
16+
#' @param variable A character string specifying the variable/column name.
17+
#' @param operator A character string specifying the operator (e.g., "==", "!=", "<", ">", "<=", ">=").
18+
#' @param value The value to compare against.
19+
#'
20+
#' @return An updated `BlockConditions` object with the new condition added.
21+
#' @method add_condition BlockConditions
22+
#' @keywords internal
23+
setGeneric("add_condition", function(object, variable, operator, value) standardGeneric("add_condition"))
24+
25+
#' Add conditions
26+
#' @keywords internal
27+
setMethod("add_condition", "BlockConditions", function(object, variable, operator, value) {
28+
object@conditions <- c(object@conditions, list(list(variable = variable, operator = operator, value = value)))
29+
object
30+
})
31+
32+
#' Get condition expression
33+
#'
34+
#' @param object A `BlockConditions` object.
35+
#' @param dataname `character(1)` The name of the dataset to filter.
36+
#' @param data The reactive `data` object from `teal`.
37+
#' @return `character(1)` The condition expression.
38+
#' @method get_str_expressions BlockConditions
39+
#' @keywords internal
40+
setGeneric("get_str_expression", function(object, dataname, data) standardGeneric("get_str_expression"))
41+
42+
#' Get condition expression
43+
#' @keywords internal
44+
setMethod("get_str_expression", "BlockConditions", function(object, dataname, data) {
45+
conds <- lapply(object@conditions, function(cond) {
46+
var <- cond$variable
47+
val <- if (is.numeric(data()[[dataname]][[cond$variable]])) {
48+
cond$value
49+
} else {
50+
paste0("'", cond$value, "'")
51+
}
52+
paste0(var, " ", cond$operator, " ", val)
53+
})
54+
paste0("(", paste(conds, collapse = " & "), ")")
55+
})

0 commit comments

Comments
 (0)