While working on a big script, I noticed some interesting behavior regarding expressions.
Consider the following piece of jsl code:
<FONT color="#339966">// Create a namespace for cleanliness</FONT>
ns = New Namespace("ns");
<FONT color="#339966">// Make an example data table dt</FONT>
ns:dt = <FONT color="#3366FF">New Table</FONT>("window",
<FONT color="#3366FF">New Column</FONT>("a", set values([1,2,3])),
<FONT color="#3366FF">New Column</FONT>("b", set values([1,4,9]))
);
<FONT color="#339966">// 1. Make an expression gr_exp1 containing the graph builder instructions</FONT>
ns:gr_exp1 = <FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << <FONT color="#0000FF">Graph Builder</FONT>(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);<BR /><FONT color="#339966">// Running the expression will build the graph (as expected)
<FONT color="#000000">ns:gr_exp1;</FONT><BR />
// 2. Make a function f1 that saves the graph builder instructions into an expression gr_exp2</FONT>
ns:f1 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:gr_exp2 = <FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << <FONT color="#0000FF">Graph Builder</FONT>(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);
);
<FONT color="#339966">// When one runs the function, the graph will be displayed, though only saving the <BR />// expression into a variable is desired.</FONT>
ns:f1;Since for this script I just wanted to save the graph as an expression (and evaluate later), I did not want it to pop-up. After some trial and error, I noticed that nesting the expression in another expression solved the problem:
ns:f2 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:gr_exp2 = <FONT color="#3366FF">expr</FONT>(<FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
));
);<BR /><FONT color="#339966">// Now no graph is displayed when running the function, and the expression is saved!</FONT><BR />ns:f2;<BR />That solved my issue. But, given the structure of my script, I actually needed to call the function f2 from another function, say f3:
<FONT color="#339966">// 4. Now make a function f3 that calls f2 (with the idea of simply saving an expression)</FONT>
ns:f3 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:f2;
);
<FONT color="#339966">// Running the script will produce a graph again...</FONT>
ns:f3;Then I noticed that modifying the function f2 to have a triple nested expression solved the issue - it saves the graph as an expression, while suppressing its evaluation:
<FONT color="#339966">// This can be solved by modifying f2</FONT>
ns:f2 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:gr_exp2 = <FONT color="#3366FF">expr</FONT>(<FONT color="#3366FF">expr</FONT>(<FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
)));
);
<FONT color="#339966">// Running f3 now acts as desired (saves expression and suppresses evaluation of graph)</FONT>
ns:f3;While I have managed to navigate through this issue, my question is:
How do I (correctly) make a function that, upon calling it, saves the expression into a variable without evaluating the expression?
I'm using JMP Pro 18.0. Any help or comments are appreciated.