assign_at_expr {lazy.symbolic} | R Documentation |
assign new expr at path
Description
Japanese help file: assign_at_expr_JPH
Usage
assign_at_expr(expr, path, value)
Arguments
expr |
A quoted expression. If a character string is given, it is parsed into an expression using 'parse(text = ...)'. |
path |
An integer vector specifying the index path in the expression tree (e.g., 'c(2, 2, 1)'). Typically obtained from [grep_expr()]. |
value |
An expression to assign at the specified path. If omitted, the function returns the sub-expression at that path instead of assigning. |
Details
'assign_at_expr()' extracts or replaces a sub-expression at a specified path in the expression tree.
Value
an expression
See Also
[grep_expr()]
Examples
(expr <- quote(tr(A %*% B) + A + C))
# Omitting value is equivalent to extracting the sub-expression
assign_at_expr(expr, c(2, 2))
expr[[2]][[2]]
assign_at_expr(expr, 2)
expr[[2]]
# Replace expr[[2]][[2]][[2]][[2]] with quote(Z)
(expr <- expr_temp <- quote(tr(A %*% B) + A + C))
assign_at_expr(expr, c(2,2,2,2), quote(Z))
expr_temp[[2]][[2]][[2]][[2]] <- quote(Z); expr_temp
# we can use the result of `grep_expr` as path
assign_at_expr(expr, grep_expr(expr, "A")[[2]]$path, quote(Z))
[Package lazy.symbolic version 1.0.0.20250830 ]