cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • New to JMP? Join us Sept. 23-24 for the Early User Edition of Discovery Summit, tailor-made for new users. Register now for free!
  • Your voice matters! Tell us how you prefer to receive JMP updates, so we can tailor our communication to your needs. Take short survey.
  • See how to access JMP Marketplace - and - find, create & share add-ins to extend your JMP. Watch video.

Discussions

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

If/Loop doesn't work inside of GraphBox

Dear JMP Users, this is a simplification of a problem I am currently encountering in a script of mine:

I am trying to encode the creation of a Graph Box inside of a function (here 'Test'), so that I can later loop over this process easily.
However, this code snippet doesn't work because the variables inside of the 'If' (showHorLines, numReferenceLines) don't seem to get evaluated properly, if at all. This is the case even though the other variables I am using in the arguments of the function (width, height) work as intended, which led me to the conclusion that it's the fault of the 'If' inside of the Graph Box.

Clear Globals();
Names Default To Here( 1 );

Test = Function({width, height, showHorLines, numReferenceLines}, {Default Local},
	gb = GraphBox(
		FrameSize(width, height),
		XScale(0, width),
		YScale(0, height),
		XName("split"),
		YName("color"),
		//[] Generate grey horizontal lines
		If(showHorLines == 1,
			PenColor(65); 
			For(h = 0, h <= numReferenceLines, h++, 
				HLine(h*height/numReferenceLines)
			);
			PenColor(0),
		);
	);
	win = NewWindow("new window", gb);
);

Test(200, 200, 1, 10);

Is there a way to resolve this issue, without changing the Graph Box into some other object?
If possible, it would be also nice for this to stay a function because I also once tried a similar approach but with Eval, EvalExpr and Expr which also didn't work and let to a similar fate.

Best Regards

1 ACCEPTED SOLUTION

Accepted Solutions
mmarchandFSLR
Level VI

Re: If/Loop doesn't work inside of GraphBox

I would suggest using Eval( Eval Expr() ).  This worked for me.

 

Names Default To Here( 1 );

Test = Function( {width, height, showHorLines, numReferenceLines},
	{Default Local},
	Eval(
		Eval Expr(
			gb = Graph Box(
				FrameSize( Expr( width ), Expr( height ) ),
				X Scale( 0, Expr( width ) ),
				Y Scale( 0, Expr( height ) ),
				XName( "split" ),
				YName( "color" ),
				If( Expr( showHorLines ) == 1,
					Pen Color( 65 );
					For( h = 0, h <= Expr( numReferenceLines ), h++,
						H Line( h * Expr( height ) / Expr( numReferenceLines ) )
					);
					Pen Color( 0 );
				,

				)
			);
			win = New Window( "new window", gb );
		)
	)
);

Test( 200, 200, 1, 10 );

View solution in original post

2 REPLIES 2
mmarchandFSLR
Level VI

Re: If/Loop doesn't work inside of GraphBox

I would suggest using Eval( Eval Expr() ).  This worked for me.

 

Names Default To Here( 1 );

Test = Function( {width, height, showHorLines, numReferenceLines},
	{Default Local},
	Eval(
		Eval Expr(
			gb = Graph Box(
				FrameSize( Expr( width ), Expr( height ) ),
				X Scale( 0, Expr( width ) ),
				Y Scale( 0, Expr( height ) ),
				XName( "split" ),
				YName( "color" ),
				If( Expr( showHorLines ) == 1,
					Pen Color( 65 );
					For( h = 0, h <= Expr( numReferenceLines ), h++,
						H Line( h * Expr( height ) / Expr( numReferenceLines ) )
					);
					Pen Color( 0 );
				,

				)
			);
			win = New Window( "new window", gb );
		)
	)
);

Test( 200, 200, 1, 10 );
Craige_Hales
Super User

Re: If/Loop doesn't work inside of GraphBox

@nozellot  quick explanation: the graphbox will continue to exist long after the function Test exits. The parameters to Test vanish when Test exits. The script attached to the graphbox won't be able to find the parameters later.

@mmarchandFSLR  is using my preferred solution to convert the parameters to their values. By identifying them with

Expr( showHorLines )

and wrapping that with

Eval Expr( ... )

which evaluates all of the expr() and leaves the evaluated answers behind, then using

Eval( ... )

to run the GraphBox() function with the expression that holds constants instead of variables, you get a graph box that is not dependent on parameters that are no longer available.

 

Craige

Recommended Articles