Function Repository Resource:

ExpressionViewer

Source Notebook

A dynamic tool that helps visualize the structure of an expression

Contributed by: Michael Sollami

ResourceFunction["ExpressionViewer"][expr]

produces a dynamic graphical view of an expression expr to help explore its structure.

Details and Options

ResourceFunction["ExpressionViewer"] prints the position of whatever subexpression is clicked.
ResourceFunction["ExpressionViewer"] dynamically displays the position of the current subexpression under the mouse in the notebook's status area.
Upon each click, ResourceFunction["ExpressionViewer"] copies a list of positions into the clipboard.
ResourceFunction["ExpressionViewer"] only hides the heads of symbols for which AtomQ gives True.

Examples

Basic Examples (2) 

ExpressionViewer helps inspect the nested structure of any valid expression:

In[1]:=
ResourceFunction["ExpressionViewer"][{1 + 1, bar["a", "b"]}]
Out[1]=

Click the part of the expression you want to address to print the part specification:

In[2]:=
ResourceFunction["ExpressionViewer"][{1 + 1, bar["a", "b"]}]
Out[2]=

Visually find the position of any part of a deeply nested expression by clicking it:

In[3]:=
expr = {1 -> 2, <|
    "3" -> {1 -> 2, <|"3" -> 4, "5" -> 6|>, {1, <|"3" -> 4, "5" -> 6|>,
        7, 3}}, "5" -> 6|>, {1, <|"3" -> 4, "5" -> 6|>, {"findme", 2, 3}, 3}};
ResourceFunction["ExpressionViewer"][expr]
Out[4]=

After you click "findme" in the previous cell, the position is printed and copied to the clipboard, and then Extract can be used:

In[5]:=
Extract[expr, {3, 3, 1}]
Out[5]=

Options (4) 

ColorFunction (3) 

Supply a custom ColorFunction that colors the parts based on depth:

In[6]:=
expr = {1 -> foo[2], bar["a", "b"]};
ResourceFunction["ExpressionViewer"][expr, ColorFunction -> (RGBColor[0, 1, 0, 0.2] &)]
Out[7]=

Plot with built-in gradients like "DeepSeaColors":

In[8]:=
expr = Nest[f[#] &, x, 10];
ResourceFunction["ExpressionViewer"][expr, ColorFunction -> ColorData["DeepSeaColors"]]
Out[9]=

Make a funky plot with the "Hue" style:

In[10]:=
expr = {1 -> 2, <|
    "3" -> {1 -> 2, <|"3" -> 4, "5" -> 6|>, {1, <|"3" -> 4, "5" -> 6|>,
        7, 3}}, "5" -> 6|>, {1, <|"3" -> 4, "5" -> 6|>, {1, 2, 3}, 3}};
ResourceFunction["ExpressionViewer"][expr, ColorFunction -> "Hue"]
Out[11]=

FrameMargins (1) 

Set the margins for the frames:

In[12]:=
expr = Nest[(1 + #)^2 &, x, 3];
Manipulate[
 ResourceFunction["ExpressionViewer"][expr, FrameMargins -> m], {m, 0,
   10}]
Out[13]=

Publisher

Michael Sollami

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 01 March 2019

Related Resources

License Information