|
| 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