cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
shuey
Level I

How to selectively evaluate variables in New Script() while preserving Expr() for deferred evaluation?

I'm creating a data table script using New Script() that needs to be evaluated later when a user clicks it. I need to substitute some variables at script-creation time (like a file path) while preserving Expr() calls that should only be evaluated when the script actually runs.

 

file_path = "C:\MyData\slopes.jmp";

dt << New Script("Plot Slopes",
    dt_slopes = Open( file_path );
    slope_array = dt_slopes:slope << Get Values;
    
    nw = New Window( "Slope Plots",
        gb = Graph Box()
    );
    
    framebox = gb[Frame Box(1)];
    
    For Each( {slope}, slope_array,
        Eval( Eval Expr(
            framebox << Add Graphics Script(
                Y Function( Expr(slope) * x, x );
            );
        ));
    );
);

The problem: I need file_path to be evaluated and inserted as a literal string when creating the script, but the Expr(slope) inside the For Each loop must remain unevaluated until the script runs (when the user clicks it).

If I wrap everything in Eval(Eval Expr()) to substitute file_path, it also evaluates Expr(slope)prematurely, causing an error since slope doesn't exist yet.

What's the correct approach to selectively evaluate some variables while preserving others for deferred evaluation in stored data table scripts?

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to selectively evaluate variables in New Script() while preserving Expr() for deferred evaluation?

One option is to add some Expr(Name Expr()) to your code

Names Default To Here(1);

file_path = "C:\MyData\slopes.jmp";

Eval(EvalExpr(
	dt << New Script(
		"Plot Slopes",
		dt_slopes = Open(Expr(file_path));
		slope_array = dt_slopes:slope << Get Values;
		
		nw = New Window("Slope Plots", gb = Graph Box());
		
		framebox = gb[Frame Box(1)];
		
		Expr(Name Expr(For Each({slope}, slope_array,
			Eval(Eval Expr(framebox << Add Graphics Script(Y Function(Expr(slope) * x, x))))
		)));
	);
));

When you run only the EvalExpr part you will end up with following script

dt << New Script(
	"Plot Slopes",
	dt_slopes = Open("C:\MyData\slopes.jmp");
	slope_array = dt_slopes:slope << Get Values;
	nw = New Window("Slope Plots", gb = Graph Box());
	framebox = gb[Frame Box(1)];
	For Each({slope}, slope_array,
		Eval(
			Eval Expr(
				framebox << Add Graphics Script(Y Function(Expr(slope) * x, x))
			)
		)
	);
)

jthi_0-1760622928220.png

 

-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: How to selectively evaluate variables in New Script() while preserving Expr() for deferred evaluation?

One option is to add some Expr(Name Expr()) to your code

Names Default To Here(1);

file_path = "C:\MyData\slopes.jmp";

Eval(EvalExpr(
	dt << New Script(
		"Plot Slopes",
		dt_slopes = Open(Expr(file_path));
		slope_array = dt_slopes:slope << Get Values;
		
		nw = New Window("Slope Plots", gb = Graph Box());
		
		framebox = gb[Frame Box(1)];
		
		Expr(Name Expr(For Each({slope}, slope_array,
			Eval(Eval Expr(framebox << Add Graphics Script(Y Function(Expr(slope) * x, x))))
		)));
	);
));

When you run only the EvalExpr part you will end up with following script

dt << New Script(
	"Plot Slopes",
	dt_slopes = Open("C:\MyData\slopes.jmp");
	slope_array = dt_slopes:slope << Get Values;
	nw = New Window("Slope Plots", gb = Graph Box());
	framebox = gb[Frame Box(1)];
	For Each({slope}, slope_array,
		Eval(
			Eval Expr(
				framebox << Add Graphics Script(Y Function(Expr(slope) * x, x))
			)
		)
	);
)

jthi_0-1760622928220.png

 

-Jarmo
hogi
Level XIII

Re: How to selectively evaluate variables in New Script() while preserving Expr() for deferred evaluation?

nice trick:

expr(name expr())  as a synonym for  protect()


maybe vote here:

2 functions for the 2 meanings of expr() 

Recommended Articles