Evaluate a Comparison Expression Without eval(parse())
Source:R/forestsearch_helpers.R
evaluate_comparison.RdParses a string of the form "var op value" and evaluates it
directly against a data frame column using operator dispatch. Falls back
to column-name lookup for bare names.
Details
Supported operators (matched longest-first to avoid partial-match
ambiguity): <=, >=, !=, ==, <,
>.
If no operator is found, expr is treated as a column name and
the result is df[[expr]] == 1.
The value on the right-hand side is coerced to numeric when possible, otherwise kept as character for string comparisons.
Examples
if (FALSE) { # \dontrun{
df <- data.frame(er = c(-1, 0, 1, 2), size = c(10, 20, 30, 40))
evaluate_comparison("er <= 0", df)
# [1] TRUE TRUE FALSE FALSE
evaluate_comparison("size > 25", df)
# [1] FALSE FALSE TRUE TRUE
} # }