Title: | Programmatic Way to Construct Complex Filtering Queries |
---|---|
Description: | Syntax for defining complex filtering expressions in a programmatic way. A filtering query, built as a nested list configuration, can be easily stored in other formats like 'YAML' or 'JSON'. What's more, it's possible to convert such configuration to a valid expression that can be applied to popular 'dplyr' package operations. |
Authors: | Krystian Igras [aut, cre], Damien Sorel [cph] (Syntax for defining queries using rules and groups as included in 'jQuery-QueryBuilder' JavaScript framework.) |
Maintainer: | Krystian Igras <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-10-25 06:47:39 UTC |
Source: | CRAN |
Combine two lists
attach_to_list(base_list, extra_list)
attach_to_list(base_list, extra_list)
base_list |
List to attach objects to. |
extra_list |
List from which elements should be attached to |
List.
Generate error message
err_msg(msg, ..., .envir = parent.frame())
err_msg(msg, ..., .envir = parent.frame())
msg |
Character string interpreted by glue. |
... |
Extra arguments passed to glue. |
.envir |
Environment to evaluate each expression in, passed to glue. |
Executed error with interpolated message.
Check if value fits to a range
in_range(x, range)
in_range(x, range)
x |
Numeric value. |
range |
Vector of length 2, storing range change limits. |
A logical vector indicating which elements of x
fit into the specified range
.
Check if character value matches the provided pattern
in_string(x, pattern, ...)
in_string(x, pattern, ...)
x |
String value. |
pattern |
Pattern that should be matched to |
... |
Extra arguments passed to grepl. |
A logical vector indicating which elements of x
are matching the provided pattern
.
Compare the string to empty value
is_empty(x)
is_empty(x)
x |
String value. |
A logical vector indicating which elements equal ""
.
Extract attribute of each element from a set of lists
lget_attr(list_obj, attribute)
lget_attr(list_obj, attribute)
list_obj |
List of lists. Each nested list should contain |
attribute |
Name of the attribute to extract from each object. |
Vector of the same length, storing extracted attributes.
Required due to erroneous operations on objects with names such as 'in' or 'for'.
operator_name_prefix
operator_name_prefix
An object of class character
of length 1.
Rename operators with the provided prefix
prefix_operators_name(operators)
prefix_operators_name(operators)
operators |
List storing queryOperators. |
Convert query definition to expression
query_to_expr_bare(query, operators, conditions, keep_na)
query_to_expr_bare(query, operators, conditions, keep_na)
query |
Query definition (see queryRule and queryGroup). |
operators |
List storing queryOperators. |
conditions |
List storing queryConditions. |
keep_na |
Should each rule expression be extended with rule excluding/including 'NA' values? |
Character value storing expression to be parsed.
Condition is two-argument function such as '|' or '&' used to combine pair of rules.
queryCondition(method) setQueryConditions(..., .queryBuilderConfig = queryBuilderConfig) listQueryConditions(.queryBuilderConfig = queryBuilderConfig, print = TRUE) default_conditions
queryCondition(method) setQueryConditions(..., .queryBuilderConfig = queryBuilderConfig) listQueryConditions(.queryBuilderConfig = queryBuilderConfig, print = TRUE) default_conditions
method |
R function of two parameters that is used to combine a pair of rules. |
... |
Name-value pairs defining condition name and method respectively.
Should be defined with usage of |
.queryBuilderConfig |
R6 class object storing query configuration. See queryBuilderConfigClass. |
print |
Should the list of operators be printed into console? |
An object of class list
of length 2.
queryCondition
: defines condition method.
setQueryConditions
:
is used to register the defined conditions in the default or custom queryBuilderConfigClass object.
listQueryConditions
: returns list of registered conditions.
default_conditions
: an object storing default definitions for conditions.
setQueryConditions( "XOR" = queryCondition(xor) ) query <- queryGroup( condition = "XOR", queryRule("am", "equal", 1), queryRule("vs", "equal", 1) ) queryToExpr(query)
setQueryConditions( "XOR" = queryCondition(xor) ) query <- queryGroup( condition = "XOR", queryRule("am", "equal", 1), queryRule("vs", "equal", 1) ) queryToExpr(query)
Operator are functions of maximum two arguments. The first argument is interpreted as a field (e.g. column name), the second one as a filtering value interpreted by operator accordingly. Some operators, such as 'is_empty' (that compares field values to empty string) don't require any value provided.
queryOperator(method) setQueryOperators(..., .queryBuilderConfig = queryBuilderConfig) listQueryOperators(.queryBuilderConfig = queryBuilderConfig, print = TRUE) default_operators
queryOperator(method) setQueryOperators(..., .queryBuilderConfig = queryBuilderConfig) listQueryOperators(.queryBuilderConfig = queryBuilderConfig, print = TRUE) default_operators
method |
R function the operator should be transformed to when parsing result to R expression.
The function should take at most two parameters. The first one (obligatory) is variable vector,
the second one additional parameters interpreted by operator.
Could be negated with exclamation mark e.g. |
... |
Name-value pairs defining operator name and method respectively.
Should be defined with usage of |
.queryBuilderConfig |
R6 class object storing query configuration. See queryBuilderConfigClass. |
print |
Should the list of operators be printed into console? |
An object of class list
of length 20.
Operators are stored as quotes, that are further interpreted while converting the query to filtering expression.
queryOperator
: defines a custom operator that can be used in generated query.
setQueryOperators
:
is used to register the defined operators in the default or custom queryBuilderConfigClass object.
listQueryOperators
: allows to list available operators for the specific column type.
default_operators
: an object storing default definitions for operators.
A single 'quote' storing the provided method.
listQueryOperators() in_closed_range <- function(x, range) { x >= range[1] & x <= range[2] } setQueryOperators( "within" = queryOperator(in_closed_range), "not_within" = queryOperator(!in_closed_range) ) query <- queryGroup( condition = "AND", queryRule("am", "equal", 1), queryRule("qsec", "within", c(10, 15)), queryRule("disp", "not_within", c(10, 15)) ) queryToExpr(query)
listQueryOperators() in_closed_range <- function(x, range) { x >= range[1] & x <= range[2] } setQueryOperators( "within" = queryOperator(in_closed_range), "not_within" = queryOperator(!in_closed_range) ) query <- queryGroup( condition = "AND", queryRule("am", "equal", 1), queryRule("qsec", "within", c(10, 15)), queryRule("disp", "not_within", c(10, 15)) ) queryToExpr(query)
Query is configuration consisting of rules and group. Rule defines a single filtering expression whereas group is combining multiple rules (or nested groups) with the provided condition.
queryGroup(..., condition = "AND") queryRule(field, operator, value = NULL, ...)
queryGroup(..., condition = "AND") queryRule(field, operator, value = NULL, ...)
... |
Rules defined with |
condition |
Group condition. By default 'AND' and 'OR' are available. To set custom one use setQueryConditions. |
field |
Field of the filter applied to the rule. To set custom one use setQueryOperators. |
operator |
Name of the operator to be applied to the rule. |
value |
(optional) Values that should be applied to the rule. Some operators, such as 'is_null', don't require any value provided. |
Having the example expression 'a == 1 | (vs == 0 & qsec > 10)' we can distinct the following rules and groups:
Rules: - 'am == 1' - related to 'am' field, applies '==' operator with '1' value, - 'vs == 0' - related to 'vs' field, applies '==' operator with '1' value, - 'qsec > 10' - related to 'qsec' field, applies '>' operator with '10' value.
Groups: - '(vs == 0 & qsec > 10)' - combines two rules ('vs == 0' and 'qsec > 10') with '&' condition, - 'a == 1 | (vs == 0 & qsec > 10)' - combines rule 'a == 1' and group '(vs == 0 & qsec > 10)' with '|' condition.
Such query can be defined by 'queryBuilder' the following way:
queryGroup(
condition = "OR",
queryRule("am", "equal", 1)
queryGroup(
condition = "AND",
queryRule("vs", "equal", 0),
queryRule("qsec", "greater", 10)
)
)
Connection between conditions and operators names and their R-based counterparts are defined with queryBuilderConfig class.
The defined query can be then converted to filtering expression with queryToExpr function.
Nested lists structure.
queryGroup( condition = "OR", queryRule("am", "equal", 1), queryGroup( condition = "AND", queryRule("vs", "equal", 0), queryRule("qsec", "greater", 10) ) )
queryGroup( condition = "OR", queryRule("am", "equal", 1), queryGroup( condition = "AND", queryRule("vs", "equal", 0), queryRule("qsec", "greater", 10) ) )
Default object storing 'queryBuilder' configuration.
queryBuilderConfig
queryBuilderConfig
An object of class queryBuilderConfig
(inherits from R6
) of length 8.
R6 class representing 'queryBuilderConfig' object.
R6 class representing 'queryBuilderConfig' object.
The object is responsible for storing definitions for operators and conditions that are used to generate query expression. It also allows to manage its objects by the provided methods.
R6 Class constructor for query configuration (operators, conditions and methods for managing the objects).
new()
Create queryBuilderConfig object with initialized conditions and operators.
queryBuilderConfigClass$new( conditions = default_conditions, operators = default_operators, ... )
conditions
Conditions.
operators
Operators.
...
Unused.
The object of class 'queryBuilderConfig'.
add()
Add conditions and conditions to 'queryBuilderConfig' object.
queryBuilderConfigClass$add(conditions = NULL, operators = NULL)
conditions
Conditions.
operators
Operators.
remove()
Remove conditions or operators from 'queryBuilderConfig' object.
queryBuilderConfigClass$remove(conditions_id = NULL, operators_id = NULL)
conditions_id
Id of conditions to remove.
operators_id
Id of operators to remove.
get_from_private()
Get private elements from 'queryBuilderConfig' object.
queryBuilderConfigClass$get_from_private(name)
name
Name of the element to get.
set_to_private()
Set private elements to 'queryBuilderConfig' object.
queryBuilderConfigClass$set_to_private(name, value)
name
Name of the element to set.
value
New element value.
reset()
Restore default conditions and conditions of 'queryBuilderConfig' object and clear out remaining private objects.
queryBuilderConfigClass$reset()
clone()
The objects of this class are cloneable with this method.
queryBuilderConfigClass$clone(deep = FALSE)
deep
Whether to make a deep clone.
The function takes a list of condition rules provided by the widget (input[[<widget-name>]]
) and
returns valid R expression that can be used for example in filter function.
queryToExpr(query, keep_na = FALSE, .queryBuilderConfig = queryBuilderConfig)
queryToExpr(query, keep_na = FALSE, .queryBuilderConfig = queryBuilderConfig)
query |
Query definition (see queryRule and queryGroup). |
keep_na |
Should query keep or exclude missing values? |
.queryBuilderConfig |
R6 class object storing query configuration. See queryBuilderConfigClass. |
Object of class 'call'. A filtering expression that can be passed to 'dplyr'-based filtering methods.
query <- queryGroup( condition = "AND", queryGroup( queryRule( field = "Species", operator = "equal", value = "setosa" ), queryRule( field = "Petal.Length", operator = "less", value = 1.2 ) ) ) queryToExpr(query) dplyr::filter(iris, !!queryToExpr(query))
query <- queryGroup( condition = "AND", queryGroup( queryRule( field = "Species", operator = "equal", value = "setosa" ), queryRule( field = "Petal.Length", operator = "less", value = 1.2 ) ) ) queryToExpr(query) dplyr::filter(iris, !!queryToExpr(query))
Remove list elements by their names
remove_by_name(list_obj, ids)
remove_by_name(list_obj, ids)
list_obj |
List object. |
ids |
Objects names to be removed. |
List.
Convert rule definition to expression
rule_to_expr(rule, operators, keep_na = FALSE)
rule_to_expr(rule, operators, keep_na = FALSE)
rule |
Rule definition (see queryRule). |
operators |
List storing queryOperators. |
keep_na |
Should the expression be extended with rule excluding/including 'NA' values? |
Character value storing expression to be parsed.
Substitute expression stored as a variable
substitute_q(x, env)
substitute_q(x, env)
x |
Expression to be substituted. |
env |
List of arguments to substitute for x. |