Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

recreate

Facilitate expression substitution


Description

Utility function based on substitute(), to recover an unquoted input.

Usage

recreate(x, snames = NULL)

Arguments

x

A substituted input.

snames

A character string containing set names.

Details

This function is especially useful when users have to provide lots of quoted inputs, such as the name of the columns from a data frame to be considered for a particular function.

This is actually one of the main uses of the base function substitute(), but here it can be employed to also detect SOP (sum of products) expressions, explained for instance in function translate().

Such SOP expressions are usually used in contexts of sufficieny and necessity, which are indicated with the usual signs -> and <-. These are both allowed by the R parser, indicating standard assignment. Due to the R's internal parsing system, a sufficient expression using -> is automatically flipped to a necessity statement <- with reversed LHS to RHS, but this function is able to determine what is the expression and what is the output.

The other necessity code <= is also recognized, but the equivalent sufficiency code => is not allowed in unquoted expressions.

Value

A quoted, equivalent expression or a substituted object.

Author(s)

Adrian Dusa

See Also

Examples

recreate(substitute(A + ~B*C))

foo <- function(x, ...) recreate(substitute(list(...)))

foo(arg1 = 3, arg2 = A + ~B*C)

df <- data.frame(A = 1, B = 2, C = 3, Y = 4)

# substitute from the global environment
# the result is the builtin C() function
res <- recreate(substitute(C))

is.function(res) # TRUE

# search first within the column name space from df
recreate(substitute(C), colnames(df))
# "C"

# necessity well recognized
recreate(substitute(A <- B))

# but sufficiency is flipped
recreate(substitute(A -> B))

# more complex SOP expressions are still recovered
recreate(substitute(A + ~B*C -> Y))

admisc

Adrian Dusa's Miscellaneous

v0.12
GPL (>= 3)
Authors
Adrian Dusa [aut, cre, cph] (<https://orcid.org/0000-0002-3525-9253>)
Initial release
2021-03-16

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.