These functions allow you to extract and/or modify a subexpression within an expression.
Usage
expr_sub(expr, idx, env = parent.frame())
expr_sub(expr, idx, env = parent.frame()) <- valueArguments
- expr
The expression to select from. Can also be a list of expressions, in which case the first element of
indexselects the expression from the list.- idx
A valid index:
NULLor an integer vector.- env
Environment for any injections in
expr(see expression).- value
Replacement; an expression.
Details
The elixir functions expr_match() and expr_locate() find matching
"subexpressions" within expressions and return indices that allow accessing
these subexpressions. For example, the expression 1 + 2 + 3 contains all
the following subexpressions:
| index | subexpression | accessed with R code |
NULL | 1+2+3 | expr |
1 | + | expr[[1]] |
2 | 1+2 | expr[[2]] |
3 | 3 | expr[[3]] |
c(2,1) | + | expr[[2]][[1]] or expr[[c(2, 1)]] |
c(2,2) | 1 | expr[[2]][[2]] or expr[[c(2, 2)]] |
c(2,3) | 2 | expr[[2]][[3]] or expr[[c(2, 3)]] |
Any index returned by expr_match() or expr_locate() will either be
NULL (meaning the whole expression / expression list) or an integer vector
(e.g. 1 or c(2,3) in the table above).
Suppose you have an index, idx. If idx is an integer vector, you can
just use expr[[idx]] to access the subexpression. But in the case where
idx is NULL, R will complain that you are trying to select less than one
element. The sole purpose of expr_sub() is to get around that issue and
allow you to pass either NULL or an integer vector as the index you want
for an expression or list of expressions.
See also
expr_match(), expr_locate() which return indices to
subexpressions.