Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^renv$
^renv\.lock$
^LICENSE\.md$
^\.lintr
^docs/
Expand All @@ -8,4 +10,4 @@
^docs$
^pkgdown$
^\.github$
^_HEXAGON_DESIGN_CONTEST\.md$
^_HEXAGON_DESIGN_CONTEST\.md$
15 changes: 11 additions & 4 deletions R/BlockConditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ setClass("BlockConditions", slots = list(conditions = "list"))
#'
#' @param object A `BlockConditions` object.
#' @param variable A character string specifying the variable/column name.
#' @param operator A character string specifying the operator (e.g., "==", "!=", "<", ">", "<=", ">=").
#' @param operator A character string specifying the operator (e.g., "==", "!=", "<", ">", "<=", ">=", "%in%", "!%in%").
#' @param value The value to compare against.
#'
#' @return An updated `BlockConditions` object with the new condition added.
Expand Down Expand Up @@ -46,10 +46,17 @@ setMethod("get_str_expression", "BlockConditions", function(object, dataname, da
var <- cond$variable
val <- if (is.numeric(data()[[dataname]][[cond$variable]])) {
cond$value
} else if (isTRUE(cond$operator == "%in%" || cond$operator == "!%in%")) {
quoted_vals <- paste0(sprintf("'%s'", cond$value), collapse = ", ")
sprintf("c(%s)", quoted_vals)
} else {
paste0("'", cond$value, "'")
sprintf("'%s'", cond$value)
}
if (cond$operator == "!%in%") {
sprintf("!(%s %%in%% %s)", var, val)
} else {
sprintf("%s %s %s", var, cond$operator, val)
}
paste0(var, " ", cond$operator, " ", val)
})
paste0("(", paste(conds, collapse = " & "), ")")
sprintf("(%s)", paste(conds, collapse = " & "))
})
62 changes: 50 additions & 12 deletions R/or_filtering_transformator.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' - **Supported Data Types & Expressions:**
#' - Supports filtering on \code{character}, \code{factor}, and \code{numeric} columns.
#' - Conditions can use operators: \code{==}, \code{!=}, \code{<}, \code{>}, \code{<=}, \code{>=}.
#' - Conditions can use operators: \code{==}, \code{!=}, \code{<}, \code{>}, \code{<=}, \code{>=}, \code{%in%}, \code{!%in%}.

Check warning on line 21 in R/or_filtering_transformator.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/or_filtering_transformator.R,line=21,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
#' - Conditions are specified as simple expressions, e.g.,
#' \code{columnA == 'value'}
#' \code{columnB != 5}
Expand Down Expand Up @@ -198,9 +198,8 @@
"Select Condition",
choices = c("==", "!=", "<", ">")
),
shiny::selectInput(
session$ns(paste0("value_input_", view_model$alt_id())), "Select Value",
choices = NULL
shiny::uiOutput(
session$ns(paste0("value_input_", view_model$alt_id()))
),
shiny::actionButton(
session$ns(paste0("add_condition_", view_model$alt_id())),
Expand All @@ -220,7 +219,17 @@
operator <- input[[paste0("operator_selector_", view_model$alt_id())]]
value <- input[[paste0("value_input_", view_model$alt_id())]]

cond_str <- paste0(variable, " ", operator, " ", value)
cond_str <- switch(operator,
"%in%" = {
quoted_vals <- paste0(sprintf("'%s'", value), collapse = ", ")
sprintf("%s %%in%% c(%s)", variable, quoted_vals)
},
"!%in%" = {
quoted_vals <- paste0(sprintf("'%s'", value), collapse = ", ")
sprintf("!%s %%in%% c(%s)", variable, quoted_vals)
},
sprintf("%s %s %s", variable, operator, value)
)

# Check for duplicates in current block
existing_conds <- if (is.null(view_model$block_conditions[[as.character(view_model$alt_id())]])) {
Expand Down Expand Up @@ -311,17 +320,46 @@
col_data <- data()[[dataname]][[selected_col]]

if (is.numeric(col_data)) {
shiny::updateSelectInput(session, paste0("value_input_", current_id), choices = c(unique(col_data)))
output[[paste0("value_input_", current_id)]] <- renderUI({
shiny::selectInput(
session$ns(paste0("value_input_", current_id)),
"Select Value",
choices = unique(col_data)
)
})
shiny::updateSelectInput(
session, paste0("operator_selector_", current_id),
choices = c("==", "!=", "<=", ">=")
session,
paste0("operator_selector_", current_id),
choices = c("==", "!=", "<=", ">="),
selected = "=="
)
} else if (is.character(col_data) || is.factor(col_data)) {
lvls <- levels(factor(col_data))

output[[paste0("value_input_", current_id)]] <- renderUI({
op <- input[[paste0("operator_selector_", current_id)]]
if (!is.null(op) && op %in% c("%in%", "!%in%")) {
shinyWidgets::pickerInput(
session$ns(paste0("value_input_", current_id)),
"Select Values",
choices = lvls,
selected = lvls,
multiple = TRUE
)
} else {
shiny::selectInput(
session$ns(paste0("value_input_", current_id)),
"Select Value",
choices = lvls
)
}
})

shiny::updateSelectInput(
session, paste0("value_input_", current_id),
choices = c(levels(factor(col_data)))
session,
paste0("operator_selector_", current_id),
choices = c("==", "!=", "%in%", "!%in%")
)
shiny::updateSelectInput(session, paste0("operator_selector_", current_id), choices = c("==", "!="))
} else {
shiny::updateSelectInput(session, paste0("value_input_", current_id), choices = NULL)
shiny::updateSelectInput(session, paste0("operator_selector_", current_id), choices = NULL)
Expand Down Expand Up @@ -356,7 +394,7 @@
within(
data(),
{
if (filters != "" & filters != "()") {
if (filters != "" && filters != "()") {
df <- df |> dplyr::filter(!!rlang::parse_expr(filters))
}
},
Expand Down
2 changes: 1 addition & 1 deletion man/add_condition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/or_filtering_transformator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading