cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
teoten
Level II

Select table, then columns

Hi, I am new using JMP and JSL and I am having trouble creating a script that selects a table, then 2 columns and then do something with that.

 

Here is an example

 

Names Default to Here( 1 );


// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, DataTable(t) << getName);
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxItems(1))
		),
		ButtonBox("OK", OKscript)
);

OKScript = Expr(
	nw << closeWindow;
	dtName = dtlb << getSelected;
	dt = Data Table(dtName[1]);
	if(NItems(dtName) > 0,
		Print("Table selected: "||dtName[1]),
		Print("No table selected")
 )
);

/* Here I tried several options like
current data table (dt);
dt << get data table;
and other but I cannot find what fits well */


xvar = .;
yvar = .;
win = New Window( "Compared Values",
		<<Modal,
		// require the user to select two variables before clicking OK
		<<On Validate(
			Show( xvar, yvar );
			If( Is Missing( xvar ) | Is Missing( yvar ),
				0, // if xvar or yvar are missing, do nothing when OK is clicked
				1
			);
		),
		Text Box( " Select test values. " ),
		H List Box(
		Text Box( "Select Variable " ),
		x = Col List Box(
			dt, // data table reference
			all, // display all columns from the data table
			xvar = (x << Get Selected)[1];
			// get the name of the selected column before the window closes
			Show( xvar );
		),
		Text Box( "Select ID" ),
			y = Col List Box(
			dt,
			all,
			yvar = (y << Get Selected)[1];
			Show( yvar );
		)
	)
);

xcol = Column( dt, xvar ); // get the columns
ycol = Column( dt, yvar );

Graph Builder(
	Show Control Panel( 0 ),
	Show Legend( 0 ),
	Show Title( 1 ),
	Variables( X( ycol ), Y( xcol ) ),
	Elements( Points( X, Y, Legend( 6 ) ) )
);

In this example I am trying a simple script where I can select one of the already open tables, select 2 columns, and do a simple plot. However I cannot manage to make the program to recognise the specific table that I select.

 

In the future I will need to interact with few tables at the same time and select different columns from different tables to obtain my results. I thought that this script would be an easy start. I would be very thankful if somebody could also provide me with a short explanation on why I cannot select the table that I'm choosing in the first box, or how to improve it, together with the solution to my script.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Select table, then columns

Modal windows have some default actions with OK and Cancel buttons, that can mess up the code you think is happening.  I find it better to adopt to the default actions, and to make simple adjustments to my code to get what I want.  Here is my modification to your table selection portion of your code to get the selected data table both set as the current data table, and also to have the variable "dt" set as the pointer to the data table.

Names Default to Here( 1 );


// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, DataTable(t) << getName);
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxSelected(1), tableSelected = (dtlb << get selected)[1] )
		),
		H List Box( ButtonBox("OK"), button box("Cancel"); )
);

If( nw["button"] == -1, throw() );

current data table( data table( tableSelected ) );

dt = current data table();

@Thierry_S suggestion of using a list to solve the issue is a good one.  However, I do it a little bit differently.  I save the full reference to the data table in the list, not just the name.

Names Default to Here( 1 );


// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, data table(DataTable(t) << getName));
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxSelected(1), dt = dtNames[(dtlb << get selected indices)[1]]
		),
		ButtonBox("OK"))
);

Show (dt);

// It allows for easy referencing in a loop to deal with referencing multiple data tables

For( i=1,i<=nitems(dtNames),i++,
	current data table(dtNames[i]);
	show(current data table());
);
Jim

View solution in original post

6 REPLIES 6
Thierry_S
Super User

Re: Select table, then columns

Hi,

 

It looks like you may have complicated things by creating a subscript to assign the output of dtlb to dt.

 

I wonder of the following variation would work better for you (no need to have the "OKScript" anymore

Names Default to Here( 1 );


// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, DataTable(t) << getName);
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxItems(1))
		),
		ButtonBox("OK", dt = dtlb << get selected)
);

Show (dt);
Thierry R. Sornasse
teoten
Level II

Re: Select table, then columns

Thank you very much, but it does not solve my problem. Actually I had a script like this before, and I started complicating things because I cannot manage that my Col List Box update the list of columns to the selected data table. Maybe my problem is in that part of the code?
txnelson
Super User

Re: Select table, then columns

Modal windows have some default actions with OK and Cancel buttons, that can mess up the code you think is happening.  I find it better to adopt to the default actions, and to make simple adjustments to my code to get what I want.  Here is my modification to your table selection portion of your code to get the selected data table both set as the current data table, and also to have the variable "dt" set as the pointer to the data table.

Names Default to Here( 1 );


// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, DataTable(t) << getName);
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxSelected(1), tableSelected = (dtlb << get selected)[1] )
		),
		H List Box( ButtonBox("OK"), button box("Cancel"); )
);

If( nw["button"] == -1, throw() );

current data table( data table( tableSelected ) );

dt = current data table();

@Thierry_S suggestion of using a list to solve the issue is a good one.  However, I do it a little bit differently.  I save the full reference to the data table in the list, not just the name.

Names Default to Here( 1 );


// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, data table(DataTable(t) << getName));
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxSelected(1), dt = dtNames[(dtlb << get selected indices)[1]]
		),
		ButtonBox("OK"))
);

Show (dt);

// It allows for easy referencing in a loop to deal with referencing multiple data tables

For( i=1,i<=nitems(dtNames),i++,
	current data table(dtNames[i]);
	show(current data table());
);
Jim
Thierry_S
Super User

Re: Select table, then columns

Hi,

 

I found additional issues with your original script that I think I have resolved (e.g. incorrect reference to the data table in your Col List Box)

Here is my solution that still may be a bit clunky from a coding perspective:

Names Default to Here( 1 );
show (dt);

// Make list of current tables
dtNames = {};

For(t=1, t<=NTable(), t++,
	InsertInto(dtNames, DataTable(t) << getName);
);

// Select the table
nw = New Window("Test", <<Modal,
		PanelBox("Pick a table",
			dtlb = ListBox(dtNames, MaxItems(1))
		),
		ButtonBox("OK", dtxt = (dtlb << Get selected)[1]) // CAPTURES Data Table Name
);


/* Here I tried several options like
current data table (dt);
dt << get data table;
and other but I cannot find what fits well */


xvar = .;
yvar = .;
win = New Window( "Compared Values",
		<<Modal,
		// REMOVED the "On Validate"" portion > require the user to select two variables before clicking OK
		
		Text Box( " Select test values. " ),
		H List Box(
		Text Box( "Select Variable " ),
		x = Col List Box(
			Data Table (dtxt), // data table reference > UPDATED the call to selected data table
			all, // display all columns from the data table
			xvar = (x << Get Selected)[1];
			// get the name of the selected column before the window closes
			Show( xvar );
		),
		Text Box( "Select ID" ),
			y = Col List Box(
			Data Table (dtxt), //> UPDATED the call to selected data table
			all,
			yvar = (y << Get Selected)[1];
			Show( yvar );
		)
	)
);



Data Table (dtxt) << Graph Builder( // Added specific call to the seleted data table
						Show Control Panel( 0 ),
						Show Legend( 0 ),
						Show Title( 1 ),
						Variables( X( as column (xvar) ), Y( As Column (yvar) ) ),
						Elements( Points( X, Y, Legend( 6 ) ) )
					);
Thierry R. Sornasse
teoten
Level II

Re: Select table, then columns

Thanks a lot, your examples clarify most of my questions. My main problem was how to point at the table to make my code work, now that I see all this different options is very clear where my errors were. 

 

One more thing, when you say "Modal windows have some default actions with OK and Cancel buttons", what do you recomend me to learn more about it? I have the scripting guide but I didn't read about it, do you think I can find it there? Or do you suggest some other source?

 

Thanks!

txnelson
Super User

Re: Select table, then columns

I suggest you read about modal windows in the Scripting Guide.  Honestly, I suggest that you read the entire Scripting Guide.

     Help==>JMP Documentation Library

Jim