Safely eval a string as an expression

safe_eval(
  x,
  vars = NULL,
  replace_comma = TRUE,
  insert_products = TRUE,
  allowed_functions = getGroupMembers("Math")
)

Arguments

x

string to evaluate

vars

list with variable values

replace_comma

Replace ',' with '.' before parsing

insert_products

Make implicit products explicit

allowed_functions

functions (sequence of characters) that should not have products, e.g. cos, sin

Examples

safe_eval("2+2")
#> [1] 4