Tools to create R formulas

g3_formula(code, ...)

Arguments

code

Unevaluated code to be turned into a formula

...

Named items to add to the formula's environment, or a single list / environment to use.

Details

When using ~, the local environment is attached to the code. This can leak unwanted variables into a model. This allows you to avoid the problem without resorting to local.

Value

A formula object, with environment created from .... Can then be used anywhere in gadget3 that accepts a formula.

Examples


# g3_formula is identical to defining a formula within local():
stopifnot(all.equal(
    g3_formula(x + 1, z = 44),
    local({ z = 44; ~x + 1 })
    ))

# If the code is destined for CRAN, you need to quote() to avoid check errors:
stopifnot(all.equal(
    g3_formula(quote( x + 1 ), z = 44),
    local({ z = 44; ~x + 1 })
    ))