twiddle: Overloaded tilde operator for in-place object modification.

Description Usage Arguments Details See Also Examples

Description

This function overloads the ~ operator to turn statements like ~{factor(x, levels = c("A", "B"))} into x <- factor(x, levels = c("A", "B") while leaving normal formula expressions alone.

Usage

1
"~"(x, y = NULL)

Arguments

x

anything. If x is an expression wrapped in braces, then this operator will assume it is a function call with the first argument intended for assignment of the resulting value.

y

anything. Only used for compatibility with the formula operator.

Details

In general, if we called ~{someexpr(x, ...)}, turn that into x <- someexpr(x, ...) in the parent environment. Otherwise, treat it as a formula.

This is dangerous meta-programming, but we live on the edge!

See Also

~

Examples

1
2
3
4
5
x <- c(1,2,3)
~{factor(x))} # x is now a factor(c(1,2,3), levels = c(1,2,3))

x <- c(1,2,3)
~{sum(x)} # x is now 6

robertzk/productivus documentation built on July 23, 2019, 2:22 p.m.