cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
fengzhuansha
Level II

How to properly create a button column and use variables in the button's script

JMP community expert, I'm from China.
The following problem description comes from machine translation. Please don't mind that the grammar can be a little strange.

 

I want to create a button column to write some scripts.
I mainly use the following methods to create button columns:

cb = Expr(Col Box("Title"));
for each( {v, i}, list,
    Insert Into( cb, button box("name", Script...) )
);

I inevitably use variables like "v", "I", etc. in "scripts", but I find that these variables are always present in "scripts" as variables rather than their actual values, which causes my scripts to not work properly.

I tried using Expr (), Eval (), and other functions to convert variables to actual values, but I couldn't get the desired effect.

 

I simulated a similar scenario using the BigClass.jmp sample data.


Suppose these students are participating in some kind of competition and need to be grouped by age. I want to create a button column that contains the student's name, and then click that button to display their corresponding group.
A sample script is as follows:

 

Names Default To Here( 1 );

aa_AgeGroup = [10 => "Age Group 1",
11 => "Age Group 1",
12 => "Age Group 1",
13 => "Age Group 1",
14 => "Age Group 2",
15 => "Age Group 2",
16 => "Age Group 2",
17 => "Age Group 2"];

dt = Open( "$sample_data/big class.jmp" );

list_name = dt:name << Get Values;

list_age = dt:age << Get Values;

cb_button = Expr( Col Box( "Name" ) );


fun = Function( {this, ver_age}, 	// 
	New Window( "Age Group", //
		<<modal,
		Text Box( Eval Insert( "^this << Get Button Name^ was placed in the group \!"^aa_ageGroup[ver_age]^\!"" ) )
	)
);

For Each( {{n, a}, i}, across( list_name, list_age ), 		//
	Insert Into(
		cb_button, //
		Button Box( n, <<Set Function( Function( {this}, fun( this, a ) ) ) )
	)
);


// Perhaps when the above two pieces of code are combined, the cause of the exception will be more intuitive
/*
For Each( {{n, a}, i}, across( list_name, list_age ), //
	Insert Into(
		cb_button, //
		Button Box( n,
			New Window( "Age Group", //
				<<modal,
				Text Box( n || " was placed in the group \!"" || aa_ageGroup[a] || "\!"" )
			)
		)
	)
);
*/

New Window( "testWindow", //
	Table Box( 	//
		cb_button,
		Number Col Box( "Age_Reference", list_age ),
		<<Set Scrollable( 15, 0 )
	)
);

I hope my friends can teach me how to make variables stored in the button script as actual values, thank you.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to properly create a button column and use variables in the button's script

You will have to evaluate the values into your buttons script. Just adding Eval(EvalExpr()) and few Expr() to your script should do the trick (there are also other options)

Names Default To Here(1);

aa_AgeGroup = [10 => "Age Group 1",
11 => "Age Group 1",
12 => "Age Group 1",
13 => "Age Group 1",
14 => "Age Group 2",
15 => "Age Group 2",
16 => "Age Group 2",
17 => "Age Group 2"];

dt = Open("$sample_data/big class.jmp");

list_name = dt:name << Get Values;

list_age = dt:age << Get Values;

cb_button = Expr(Col Box("Name"));


fun = Function({this, ver_age}, 	// 
	New Window("Age Group", //
		<<modal,
		Text Box(Eval Insert("^this << Get Button Name^ was placed in the group \!"^aa_ageGroup[ver_age]^\!""))
	)
);

For Each({{n, a}, i}, across(list_name, list_age), 		//
	Eval(EvalExpr(
		Insert Into(
			cb_button, //
			Button Box(Expr(n), <<Set Function(Function({this}, fun(this, Expr(a)))))
		)		
	));
);


New Window("testWindow", //
	Table Box( 	//
		cb_button,
		Number Col Box("Age_Reference", list_age),
		<<Set Scrollable(15, 0)
	)
);
-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: How to properly create a button column and use variables in the button's script

You will have to evaluate the values into your buttons script. Just adding Eval(EvalExpr()) and few Expr() to your script should do the trick (there are also other options)

Names Default To Here(1);

aa_AgeGroup = [10 => "Age Group 1",
11 => "Age Group 1",
12 => "Age Group 1",
13 => "Age Group 1",
14 => "Age Group 2",
15 => "Age Group 2",
16 => "Age Group 2",
17 => "Age Group 2"];

dt = Open("$sample_data/big class.jmp");

list_name = dt:name << Get Values;

list_age = dt:age << Get Values;

cb_button = Expr(Col Box("Name"));


fun = Function({this, ver_age}, 	// 
	New Window("Age Group", //
		<<modal,
		Text Box(Eval Insert("^this << Get Button Name^ was placed in the group \!"^aa_ageGroup[ver_age]^\!""))
	)
);

For Each({{n, a}, i}, across(list_name, list_age), 		//
	Eval(EvalExpr(
		Insert Into(
			cb_button, //
			Button Box(Expr(n), <<Set Function(Function({this}, fun(this, Expr(a)))))
		)		
	));
);


New Window("testWindow", //
	Table Box( 	//
		cb_button,
		Number Col Box("Age_Reference", list_age),
		<<Set Scrollable(15, 0)
	)
);
-Jarmo
fengzhuansha
Level II

Re: How to properly create a button column and use variables in the button's script

Thank you very much for your assistance in resolving my issue so effectively.
I had also considered utilizing Expr(), EvalExpr(), and Eval() to address this matter, but my comprehension of these functions was insufficient for solving the problem.
Your solution not only resolved the issue at hand, but also provided me with a deeper understanding of the integration of these functions.
Once again, thank you Jarmo.