Title: | Programmatic Utilities for Manipulating Formulas, Expressions, Calls, Assignments and Other R Objects |
---|---|
Description: | These utilities facilitate the programmatic manipulations of formulas, expressions, calls, assignments and other R language objects. These objects all share the same structure: a left-hand side, operator and right-hand side. This packages provides methods for accessing and modifying this structures as well as extracting and replacing names and symbols from these objects. |
Authors: | Christopher Brown [aut, cre], Decision Patterns [cph] |
Maintainer: | Christopher Brown <[email protected]> |
License: | GPL-2 | file LICENSE |
Version: | 1.7.1 |
Built: | 2024-11-10 05:45:28 UTC |
Source: | https://github.com/decisionpatterns/formula.tools |
Invert multiple elements of a multiple element object
.invert.single(x) .invert.plural(x)
.invert.single(x) .invert.plural(x)
x |
object to invert from |
.invert.single
Convers a formula to character representaion
## S3 method for class 'formula' as.character(x, ...)
## S3 method for class 'formula' as.character(x, ...)
x |
formula object |
... |
further arguments passed to or from other methods. |
Coerces formula to a character by deparsing.
A character vector
Christopher Brown
as.character( y ~ mx + b ) ## The function is currently defined as function(x) Reduce( paste, deparse(x) )
as.character( y ~ mx + b ) ## The function is currently defined as function(x) Reduce( paste, deparse(x) )
Get the environment
env(x) ## S3 method for class 'formula' env(x)
env(x) ## S3 method for class 'formula' env(x)
x |
object to get environoment from |
S3 returns the environment associated with an object.
For a formula object, 'env' returns the environment in the '.Environment' attribute.
Environment
env( lhs ~ rhs )
env( lhs ~ rhs )
lhs, rhs, op, and op.type
retrieve the various parts of R formulas,
calls, expressions, names/symbols. These functions were designed to
greatly facilitate symbolic manupulation using native R objects. Also
provided are methods to handle list of these objects.
lhs(x, ...) ## S4 method for signature 'call' lhs(x) ## S4 method for signature 'formula' lhs(x) ## S4 method for signature 'expression' lhs(x, ...) ## S4 method for signature 'list' lhs(x, ...) lhs(x) <- value ## S4 replacement method for signature 'call' lhs(x) <- value ## S4 replacement method for signature 'formula' lhs(x) <- value .replace.lhs.plural(x, value) ## S4 replacement method for signature 'expression' lhs(x) <- value ## S4 replacement method for signature 'list' lhs(x) <- value op(x) ## S4 method for signature 'formula' op(x) ## S4 method for signature 'call' op(x) ## S4 method for signature 'name' op(x) ## S4 method for signature 'expression' op(x) ## S4 method for signature 'list' op(x) op(x) <- value ## S4 replacement method for signature 'call' op(x) <- value ## S4 replacement method for signature 'formula' op(x) <- value .replace.op.plural(x, value) ## S4 replacement method for signature 'expression' op(x) <- value ## S4 replacement method for signature 'list' op(x) <- value rhs(x, ...) .rhs.singular(x) ## S4 method for signature 'call' rhs(x) ## S4 method for signature 'formula' rhs(x) ## S4 method for signature 'expression' rhs(x, ...) ## S4 method for signature 'list' rhs(x, ...) rhs(x) <- value .replace.rhs.singular(x, value) ## S4 replacement method for signature 'call' rhs(x) <- value ## S4 replacement method for signature 'formula' rhs(x) <- value .replace.rhs.plural(x, value) ## S4 replacement method for signature 'expression' rhs(x) <- value ## S4 replacement method for signature 'list' rhs(x) <- value
lhs(x, ...) ## S4 method for signature 'call' lhs(x) ## S4 method for signature 'formula' lhs(x) ## S4 method for signature 'expression' lhs(x, ...) ## S4 method for signature 'list' lhs(x, ...) lhs(x) <- value ## S4 replacement method for signature 'call' lhs(x) <- value ## S4 replacement method for signature 'formula' lhs(x) <- value .replace.lhs.plural(x, value) ## S4 replacement method for signature 'expression' lhs(x) <- value ## S4 replacement method for signature 'list' lhs(x) <- value op(x) ## S4 method for signature 'formula' op(x) ## S4 method for signature 'call' op(x) ## S4 method for signature 'name' op(x) ## S4 method for signature 'expression' op(x) ## S4 method for signature 'list' op(x) op(x) <- value ## S4 replacement method for signature 'call' op(x) <- value ## S4 replacement method for signature 'formula' op(x) <- value .replace.op.plural(x, value) ## S4 replacement method for signature 'expression' op(x) <- value ## S4 replacement method for signature 'list' op(x) <- value rhs(x, ...) .rhs.singular(x) ## S4 method for signature 'call' rhs(x) ## S4 method for signature 'formula' rhs(x) ## S4 method for signature 'expression' rhs(x, ...) ## S4 method for signature 'list' rhs(x, ...) rhs(x) <- value .replace.rhs.singular(x, value) ## S4 replacement method for signature 'call' rhs(x) <- value ## S4 replacement method for signature 'formula' rhs(x) <- value .replace.rhs.plural(x, value) ## S4 replacement method for signature 'expression' rhs(x) <- value ## S4 replacement method for signature 'list' rhs(x) <- value
x |
object from where to get/set the lhs/rhs |
... |
arguments passed to additional methods |
value |
the value to set for the lhs/rhs |
lhs
retrieves the left-hand side
rhs
retrieves the right-hand side
op
retrieves the operation
op.type
returns the type operator
There are also functions lhs.vars
and rhs.vars
. Like
all.vars
, these functions interpret the variables on the
left-hand and right-hand sides respectively.
These are simple functions for extracting the left-hand side, right-hand side, operator and operator type from formulas, expressions, calls, names/symbols and list containing these objects. lhs, rhs are only defined for formulas and calls ( and list and expressions ) that are defined with either one of the relational or tilde ('~') operators. If the object does not contain one of these operators, it will fail with a warning.
The defined operator types are defined by the operator.tools package: See
operators
and
setOperator
The lhs.vars
and rhs.vars
methods, return the variables used on
the lhs and rhs, respectively. If special formula variables are used, such
as '.', a data.frame or environment must also be provided such that the
variable list may be properly infered.
Value depends on the argument.
Methods for the non-standard "<-
" class exist and are not included in
the usage documentation because CRAN does not support S4 documentation for
this class.
Christopher Brown
terms, all.vars, all.names, operators
# FORMULA f <- A + B ~ C + D lhs(f) lhs(f) <- quote( E / F ) rhs(f) rhs(f) <- quote( G + H ) op(f) op(rhs(f)) op( quote(A) ) # NULL: op.type(f) # ONE-SIDED FORMULA f <- ~ A # lhs(f) # NULL rhs(f) # A # EXPRESSION e <- expression( A + B == C + D ) lhs(e) rhs(e) op(e) op.type(e) # CALL c <- quote( A + B > C + D ) lhs(c) lhs(c) <- quote(E) rhs(c) op(c) op.type(c) # ASSIGNMENT a <- quote( A <- B ) lhs(a) rhs(a) op(a) op.type(a)
# FORMULA f <- A + B ~ C + D lhs(f) lhs(f) <- quote( E / F ) rhs(f) rhs(f) <- quote( G + H ) op(f) op(rhs(f)) op( quote(A) ) # NULL: op.type(f) # ONE-SIDED FORMULA f <- ~ A # lhs(f) # NULL rhs(f) # A # EXPRESSION e <- expression( A + B == C + D ) lhs(e) rhs(e) op(e) op.type(e) # CALL c <- quote( A + B > C + D ) lhs(c) lhs(c) <- quote(E) rhs(c) op(c) op.type(c) # ASSIGNMENT a <- quote( A <- B ) lhs(a) rhs(a) op(a) op.type(a)
get.vars
extracts variable names from various R objects such as
formulas, expressions, calls, symbols, etc. It is very similar to
all.vars
except that all symbols, etc. are interpolated
to the names of variables.
get.vars(x, data = NULL, ...) ## S4 method for signature 'formula,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature 'call,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature 'expression,missing' get.vars(x, data = NULL, ...) ## S4 method for signature 'name,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature 'ANY,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature ''NULL',ANY' get.vars(x, data = NULL, ...) lhs.vars(x, ...) .lhs.vars(x, ..., data = NULL) ## S4 method for signature 'formula' lhs.vars(x, ..., data = NULL) ## S4 method for signature 'call' lhs.vars(x, ..., data = NULL) ## S4 method for signature 'expression' lhs.vars(x, ...) rhs.vars(x, ...) .rhs.vars(x, ..., data = NULL) ## S4 method for signature 'formula' rhs.vars(x, ..., data = NULL) ## S4 method for signature 'call' rhs.vars(x, ..., data = NULL) ## S4 method for signature 'expression' rhs.vars(x, ...)
get.vars(x, data = NULL, ...) ## S4 method for signature 'formula,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature 'call,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature 'expression,missing' get.vars(x, data = NULL, ...) ## S4 method for signature 'name,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature 'ANY,ANY' get.vars(x, data = NULL, ...) ## S4 method for signature ''NULL',ANY' get.vars(x, data = NULL, ...) lhs.vars(x, ...) .lhs.vars(x, ..., data = NULL) ## S4 method for signature 'formula' lhs.vars(x, ..., data = NULL) ## S4 method for signature 'call' lhs.vars(x, ..., data = NULL) ## S4 method for signature 'expression' lhs.vars(x, ...) rhs.vars(x, ...) .rhs.vars(x, ..., data = NULL) ## S4 method for signature 'formula' rhs.vars(x, ..., data = NULL) ## S4 method for signature 'call' rhs.vars(x, ..., data = NULL) ## S4 method for signature 'expression' rhs.vars(x, ...)
x |
object to extract vars from. |
data |
data set/list or environment on which the names are defined |
... |
arguments passed to subsequent functions
Methods/functions beginning with |
character vector of variables names in order that they appear in
x
.
get.vars( Species ~ ., iris ) get.vars( quote( Sepal.Length * Sepal.Width ), iris )
get.vars( Species ~ ., iris ) get.vars( quote( Sepal.Length * Sepal.Width ), iris )
Invert the operators in an object, usually a formula or expression
invert(x, ...) ## S4 method for signature 'call' invert(x) ## S4 method for signature 'expression' invert(x)
invert(x, ...) ## S4 method for signature 'call' invert(x) ## S4 method for signature 'expression' invert(x)
x |
function for invert |
... |
additional arguments passed other functions
functions prefixed with a |
The operand is returned with the relational operators inverted.
Christopher Brown
invert( quote( A > 5 ) ) invert( quote( A >= 5 ) ) invert( quote( A < 5 ) ) invert( quote( A <= 5 ) ) invert( quote( A == 5 ) ) invert( quote( A != 5 ) ) invert( quote( A %in% lettters[1:5] ) ) invert( quote( A %!in% letters[1:5] ) )
invert( quote( A > 5 ) ) invert( quote( A >= 5 ) ) invert( quote( A < 5 ) ) invert( quote( A <= 5 ) ) invert( quote( A == 5 ) ) invert( quote( A != 5 ) ) invert( quote( A %in% lettters[1:5] ) ) invert( quote( A %!in% letters[1:5] ) )
~x
) or two-sided (e.g. x~y
).Determine if an object is one- or two-sided.
Test whether a object (typically formula, call or expression) is one- (e.g.
~x
) or two-sided (e.g. x~y
).
is.one.sided(x, ...) ## S4 method for signature 'formula' is.one.sided(x, ...) ## S4 method for signature 'call' is.one.sided(x, ...) ## S4 method for signature 'expression' is.one.sided(x, ...) ## S4 method for signature 'list' is.one.sided(x, ...) ## S4 method for signature 'ANY' is.one.sided(x, ...) is.two.sided(x, ...) ## S4 method for signature 'formula' is.two.sided(x, ...) ## S4 method for signature 'call' is.two.sided(x, ...) ## S4 method for signature 'expression' is.two.sided(x, ...) ## S4 method for signature 'list' is.two.sided(x, ...) ## S4 method for signature 'ANY' is.two.sided(x, ...)
is.one.sided(x, ...) ## S4 method for signature 'formula' is.one.sided(x, ...) ## S4 method for signature 'call' is.one.sided(x, ...) ## S4 method for signature 'expression' is.one.sided(x, ...) ## S4 method for signature 'list' is.one.sided(x, ...) ## S4 method for signature 'ANY' is.one.sided(x, ...) is.two.sided(x, ...) ## S4 method for signature 'formula' is.two.sided(x, ...) ## S4 method for signature 'call' is.two.sided(x, ...) ## S4 method for signature 'expression' is.two.sided(x, ...) ## S4 method for signature 'list' is.two.sided(x, ...) ## S4 method for signature 'ANY' is.two.sided(x, ...)
x |
object to test for one-sidedness. |
... |
arguments passed to called functions |
These functions detect whether the formula is single- (unary) or double- sided. They work on formulas, expression, calls, assignments, etc.
is.single.sided
and is.unary
are alias for
is.single.sided
. is.double.sided
and is.binary
are
aliases for is.two.sided
.
logical; whether x
is an object is one-sided or two-sided formula.
Methods for the "<-
" class exist and are not included in the usage
documentation because CRAN does not support S4 documentation for this class.
form <- y ~ x is.one.sided(form) # is.single.sided(form) # is.unary(form) is.two.sided(form) # is.double.sided(form) # is.binary(form)
form <- y ~ x is.one.sided(form) # is.single.sided(form) # is.unary(form) is.two.sided(form) # is.double.sided(form) # is.binary(form)
Get the operator type used in an call, formula, expression, etc.
op.type(x) ## S4 method for signature 'call' op.type(x) ## S4 method for signature 'formula' op.type(x) ## S4 method for signature 'ANY' op.type(x) ## S4 method for signature 'expression' op.type(x) ## S4 method for signature 'list' op.type(x)
op.type(x) ## S4 method for signature 'call' op.type(x) ## S4 method for signature 'formula' op.type(x) ## S4 method for signature 'ANY' op.type(x) ## S4 method for signature 'expression' op.type(x) ## S4 method for signature 'list' op.type(x)
x |
object from which to extract the operator type |
a character vector of the operator type(s)
Split formulas, call and expressions into terms.
split_terms(x, recursive = FALSE)
split_terms(x, recursive = FALSE)
x |
object to split terms from |
recursive |
logical; whether to split terms recursively in parenthetical enclosed terms (depth-first). (Default: FALSE) |
Unlike the [terms()] function, 'split_terms' does not use [stats::terms()] and instead just splits 'x' into an **expression vector** of terms. (Terms are the mathematical notion of terms). The signs of the terms are preserved.
If 'recursive' is 'TRUE', splitting occurs recursively, i.e. parsing of the input descends into parenthetical expressions '(...)'.
See **examples**.
experession vector of terms
* [SO: How to split a formula](https://stackoverflow.com/questions/39155701/how-to-split-a-formula-in-r)
* [terms()]
split_terms(1) # 1 split_terms( quote(a) ) # a split_terms( quote(-a) ) # -a split_terms( quote(a+1) ) # a, 1 split_terms( quote(1+a) ) # 1, a split_terms( quote(-1+a) ) # -1, a split_terms( quote(-1-a) ) split_terms( quote(a+b+c) ) # a,b,c split_terms( quote((a+b)+1) ) # (a+b),1 split_terms( quote((a+b)+1), recursive=TRUE ) # a,b,1 split_terms( quote((a-b)+1), recursive=TRUE ) # a,-b,1 split_terms( quote(-a) ) # -a split_terms( quote(a-1) ) # a, -1 split_terms( quote(-a-1)) # -a, -1 split_terms( quote( -(a+1) ) ) # -(a+1) split_terms( quote( -(a+1) ), recursive=TRUE ) # -a,-1 split_terms( quote( ---a )) split_terms( quote( -(a+(b-(c+d)))), recursive=TRUE )
split_terms(1) # 1 split_terms( quote(a) ) # a split_terms( quote(-a) ) # -a split_terms( quote(a+1) ) # a, 1 split_terms( quote(1+a) ) # 1, a split_terms( quote(-1+a) ) # -1, a split_terms( quote(-1-a) ) split_terms( quote(a+b+c) ) # a,b,c split_terms( quote((a+b)+1) ) # (a+b),1 split_terms( quote((a+b)+1), recursive=TRUE ) # a,b,1 split_terms( quote((a-b)+1), recursive=TRUE ) # a,-b,1 split_terms( quote(-a) ) # -a split_terms( quote(a-1) ) # a, -1 split_terms( quote(-a-1)) # -a, -1 split_terms( quote( -(a+1) ) ) # -(a+1) split_terms( quote( -(a+1) ), recursive=TRUE ) # -a,-1 split_terms( quote( ---a )) split_terms( quote( -(a+(b-(c+d)))), recursive=TRUE )
terms method for call and expression objects
## S3 method for class 'call' terms(x, ...) ## S3 method for class 'expression' terms(x, ...)
## S3 method for class 'call' terms(x, ...) ## S3 method for class 'expression' terms(x, ...)
x |
A call object |
... |
Arguments passed to This S3 method returns a terms object for a call methods using a dispatch to
The terms are generated by making a rhs only call to
Some edge cases may not be supported. |
A terms object. See terms.object
for details.
Christopher Brown
terms( quote( A + B ) ) data(iris) x <- terms( quote( . - Species ) , data=iris )
terms( quote( A + B ) ) data(iris) x <- terms( quote( . - Species ) , data=iris )
toggle the sign of an expression
toggle.sign(x)
toggle.sign(x)
x |
expression |
'toggle.sign' changes the sign of an expression for '+' tp '-' and visa-versa.
toggle.sign(1:3) toggle.sign( quote(a) ) toggle.sign( quote(-a) ) exp <- expression( a, -b, -(a-b) ) toggle.sign(exp)
toggle.sign(1:3) toggle.sign( quote(a) ) toggle.sign( quote(-a) ) exp <- expression( a, -b, -(a-b) ) toggle.sign(exp)