drop_parens {lazy.symbolic}R Documentation

Remove unnecessary parentheses

Description

Japanese help file: drop_parens_JPH

Usage

drop_parens(expr, all = FALSE, in_biop = FALSE)

Arguments

expr

An expression or a character string.

all

Logical. If 'TRUE', remove parentheses as much as possible; if 'FALSE', keep parentheses necessary for precedence (default 'FALSE').

in_biop

(Internal) Flag used during recursion to indicate being directly under a binary operator. Normally not set by the user.

Details

Removes redundant parentheses from an expression. If 'all = TRUE', parentheses are removed as much as possible; if 'all = FALSE', parentheses required to preserve operator precedence are kept.

Value

an expression

Examples


modify_math_operators()
# example code
drop_parens("X")
drop_parens("(X)")
drop_parens("((X))")
drop_parens("(t((X)))")
drop_parens("(t((tr((X)))))") |> show_ast()
drop_parens("(t((tr((X)))))") |> show_ast()
drop_parens("(A*B)")

# examples for all
drop_parens("((A%*%(B*((C%*%C)))))") |> show_ast()
drop_parens("((A%*%(B*((C%*%C)))))", all = TRUE)|> show_ast()

drop_parens("((A%*%(B*((C%*%C)))))") |> show_ast()
drop_parens("((A%*%(B*((C%*%C)))))", all = TRUE)|> show_ast()



expr <- easy_parse("(X*A)%*%C") ; expr |> show_ast()
drop_parens(expr) |> show_ast()
expr <- easy_parse("C%*%(X*A)") ; expr |> show_ast()
drop_parens(expr) |> show_ast()
expr <- easy_parse("(X%*%A)*C") ; expr |> show_ast()
drop_parens(expr) |> show_ast()
expr <- easy_parse("C*(X%*%A)") ; expr |> show_ast()
drop_parens(expr) |> show_ast()
expr <- easy_parse("C%*%(X%*%A)") ; expr |> show_ast()
drop_parens(expr) |> show_ast()
expr <- easy_parse("(C%*%X)%*%A") ; expr |> show_ast()
drop_parens(expr) |> show_ast()


[Package lazy.symbolic version 1.0.0.20250830 ]