Mientras trabajaba en un script grande, noté un comportamiento interesante con respecto a las expresiones.
Considere el siguiente fragmento de código jsl:
<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;Como para este script solo quería guardar el gráfico como una expresión (y evaluarlo más tarde), no quería que apareciera. Después de algunas pruebas y errores, noté que anidar la expresión en otra expresión resolvió el problema:
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 />Eso resolvió mi problema. Pero, dada la estructura de mi script, en realidad necesitaba llamar a la función f2 desde otra función, digamos 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;Luego noté que modificar la función f2 para tener una expresión triple anidada resolvió el problema: guarda el gráfico como una expresión, mientras suprime su evaluación:
<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;Si bien he logrado resolver este problema, mi pregunta es:
¿Cómo puedo crear (correctamente) una función que, al llamarla, guarde la expresión en una variable sin evaluar la expresión?
Estoy usando JMP Pro 18.0. Agradezco cualquier ayuda o comentario.
Hola @ThomasB ,
Tenga en cuenta que la última expresión de una función se utiliza como valor de retorno.
Si solo desea asignar la expresión sin "devolverla", agregue un valor de retorno ficticio:
ns:f1 = function({},{Default Local},
ns:gr_exp2 = expr(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);
1;
);o
ns:f1 = function({},{Default Local},
ns:gr_exp2 = expr(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);
return(1);
);
Gracias por tu respuesta y el enlace, Jarmo, también son muy útiles.