Skip to contents

Returns a securer::securer_tool() that evaluates mathematical expressions safely via AST validation.

Usage

calculator_tool(max_calls = NULL)

Arguments

max_calls

Maximum number of invocations allowed. NULL (default) means unlimited.

Value

A securer_tool object.

Details

The calculator tool evaluates mathematical expressions in a restricted environment. Only the following functions and operators are allowed:

  • Arithmetic: +, -, *, /, ^, %%, %/%

  • Math: sqrt, abs, log, log2, log10, exp, ceiling, floor, round, trunc

  • Trigonometry: sin, cos, tan, asin, acos, atan

  • Aggregation: sum, mean, max, min, length

  • Utilities: c, pi

Expressions are first parsed and validated via an AST walk that rejects any function call or symbol not on the allowlist. Evaluation then occurs in a minimal environment containing only the allowed functions, with emptyenv() as its parent to prevent access to other R functionality.

Examples

# \donttest{
calc <- calculator_tool()
# Basic arithmetic
calc@fn(expression = "2 + 3 * 4")
#> [1] 14

# Math functions
calc@fn(expression = "sqrt(144) + log(exp(1))")
#> [1] 13

# With rate limiting
calc <- calculator_tool(max_calls = 100)
# }