BookmarkSubscribe
Choose Language Hide Translation Bar
dg1
dg1
Occasional Contributor

For loop in New Window

Hi! I am a very beginner JMP user, and am having trouble with getting my for loop to work.  I am trying to ask the user what type of factor each of their factors are. The number of times the question is asked depends on the number of coloumns in the jmp file. I was attempting to use a radio box, but am having trouble with the appending. Below is little snapshot of what I have done. Thanks!

Names Default to Here(1);
dt = Open("Example1.jmp");
number = ncols(dt);


FactorType = {"Continuous", "Categorical"}; 
New Window("Data Entry", << Modal, << return results,
Text Box("Type of Factor");, 
type= V list Box(),
);

For(i= 1, i <= number, i++,
type << Append(Radio Box(FactorType, Max Selected(1)));
);

0 Kudos
3 ACCEPTED SOLUTIONS

Accepted Solutions
julian
Staff

Re: For loop in New Window

Hi @dg1,

You're doing everything correctly! But, your use of <<Modal is inhibiting your script from functioning correctly. With a modal window, the remaining script is not executed until the OK button is clicked. If you remove <<modal your script will operate just fine, or alternatively, you can move the For() into the window statement. 

Names Default To Here( 1 );
dt = Open("Example1.jmp");
number = N Cols( dt );


FactorType = {"Continuous", "Categorical"};
New Window( "Data Entry",
	<<Modal,
	<<return results,
	Text Box( "Type of Factor" ),
	type = V List Box(), 

	For( i = 1, i <= number, i++,
		type << Append( Radio Box( FactorType, Max Selected( 1 ) ) )
	);

);

@julian 

julian
Staff

Re: For loop in New Window

Hi again @dg1,

Just to follow up, I looked a little more closely at your code and there were a few places where I think you'll hit a snag. The biggest is unloading the results. Each of your RadioBox() objects should have a handle so you can reference them later to grab the data. Below is one way to do that, that assigns the reference to a list, and then later steps through that list of references, sends the message to get the selected item, and stores the result to another list. This all happens when the OK button is clicked. 

 

 

Names Default To Here( 1 );
dt = Current Data Table();
number = N Cols( dt );
rbList = {};
rbResult = {};


FactorType = {"Continuous", "Categorical"};
win = New Window( "Data Entry",
	<<Modal,
	type = V List Box(), 
	
	Button Box( "OK",
		For( i = 1, i <= number, i++,
			rbResult[i] = rbList[i] << Get Selected
		);

		win << close window;
	), 
	
	
	For( i = 1, i <= number, i++,
		type << Append( Text Box( "Type of Factor" ) );
		type << Append( rbList[i] = Radio Box( FactorType ) );
	);

	
);

Print( rbResult );

 

 

If you haven't yet, I highly suggest you read the JMP Scripting Guide, Help > Books > Scripting Guide. It will be invaluable as you learn the language and the operations necessary to have a script work as you want. Also, I really enjoyed the book JSL Companion: Applications of the JMP Scripting Language. I would start with the Scripting Guide because it is included free in JMP, but the JSL Companion does provide a structured introduction to using JSL through a series of practical applications (and all the code is included). 

 

Hope this helps you along your way! Enjoy!

@julian 

julian
Staff

Re: For loop in New Window

That's a fair question, @David_Burnham! I think perhaps it depends? I use IfBox() quite a bit, though that's particularly designed for the conditional display of display boxes. I can't speak to the 'legitimacy' of jsl in these situations, but whatever the case OnOpen is a great suggestion.
0 Kudos
6 REPLIES 6
julian
Staff

Re: For loop in New Window

Hi @dg1,

You're doing everything correctly! But, your use of <<Modal is inhibiting your script from functioning correctly. With a modal window, the remaining script is not executed until the OK button is clicked. If you remove <<modal your script will operate just fine, or alternatively, you can move the For() into the window statement. 

Names Default To Here( 1 );
dt = Open("Example1.jmp");
number = N Cols( dt );


FactorType = {"Continuous", "Categorical"};
New Window( "Data Entry",
	<<Modal,
	<<return results,
	Text Box( "Type of Factor" ),
	type = V List Box(), 

	For( i = 1, i <= number, i++,
		type << Append( Radio Box( FactorType, Max Selected( 1 ) ) )
	);

);

@julian 

dg1
dg1
Occasional Contributor

Re: For loop in New Window

Thank you! Do you also know how to make sure the text box that says Type of Factor also shows up along with the Radio boxes?
0 Kudos
julian
Staff

Re: For loop in New Window

Hi @dg1,

It appears to be showing up for me at the top of the window, but just the once as you have it programmed. If you want one textbox for each column selection, you could do another append in each iteration of your for() loop: 

 

Names Default To Here( 1 );
dt = Open("Example1.jmp");
number = N Cols( dt );


FactorType = {"Continuous", "Categorical"};
New Window( "Data Entry",
	<<Modal,
	<<return results,
	type = V List Box(), 

	For( i = 1, i <= number, i++,
		type << Append( Text Box( "Type of Factor" ) );
		type << Append( Radio Box( FactorType, Max Selected( 1 ) ) );
	);

);

Screen Shot 2019-07-02 at 2.26.27 PM.png

 

0 Kudos
julian
Staff

Re: For loop in New Window

Hi again @dg1,

Just to follow up, I looked a little more closely at your code and there were a few places where I think you'll hit a snag. The biggest is unloading the results. Each of your RadioBox() objects should have a handle so you can reference them later to grab the data. Below is one way to do that, that assigns the reference to a list, and then later steps through that list of references, sends the message to get the selected item, and stores the result to another list. This all happens when the OK button is clicked. 

 

 

Names Default To Here( 1 );
dt = Current Data Table();
number = N Cols( dt );
rbList = {};
rbResult = {};


FactorType = {"Continuous", "Categorical"};
win = New Window( "Data Entry",
	<<Modal,
	type = V List Box(), 
	
	Button Box( "OK",
		For( i = 1, i <= number, i++,
			rbResult[i] = rbList[i] << Get Selected
		);

		win << close window;
	), 
	
	
	For( i = 1, i <= number, i++,
		type << Append( Text Box( "Type of Factor" ) );
		type << Append( rbList[i] = Radio Box( FactorType ) );
	);

	
);

Print( rbResult );

 

 

If you haven't yet, I highly suggest you read the JMP Scripting Guide, Help > Books > Scripting Guide. It will be invaluable as you learn the language and the operations necessary to have a script work as you want. Also, I really enjoyed the book JSL Companion: Applications of the JMP Scripting Language. I would start with the Scripting Guide because it is included free in JMP, but the JSL Companion does provide a structured introduction to using JSL through a series of practical applications (and all the code is included). 

 

Hope this helps you along your way! Enjoy!

@julian 

Highlighted
David_Burnham
Super User

Re: For loop in New Window

Is putting a for-loop in a new window function call really legitimate?  My view is that you shouldn't stick JSL in places where display boxes are expected, even if the code runs.  For a modal window, the JSL can be placed in the OnOpen event handler. 

-Dave
julian
Staff

Re: For loop in New Window

That's a fair question, @David_Burnham! I think perhaps it depends? I use IfBox() quite a bit, though that's particularly designed for the conditional display of display boxes. I can't speak to the 'legitimacy' of jsl in these situations, but whatever the case OnOpen is a great suggestion.
0 Kudos