cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
pjr1121
Level II

application scripting... Group by , variable

I am creating an application which allows the user to select a few options before running a script which involves a few SQL queries and then making some graphs... nothing too crazy.

 

A radio button control is used to set the value of a variable named "type"..  I want one of the "fit where" options for a bivariate graph to be the contents of this variable...

 

Bivariate(
Y( :Current),
X( :Voltage),
Show Points( 0 ),
Fit Where(
:type == "cell",
Fit Each Value( {Report( 0 ), Line Color( {0, 0, 255} ), Report( 0 )} )
),
Fit Where(
:type == type,  //this is troublesome line... the graph is generated but not quite right.
Fit Each Value( {Report( 0 ), Line Color( {213, 72, 87} ), Report( 0 )} )
),
By( :CellID)
);

 

 

The graph generates correctly, but the annoying thing is the resulting graph says "fit each value type == type".  How can I get it to display the contents of the variable which was used instead of it's name??  I've tried a few options already including "eval(type)".

Capture.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
gzmorgan0
Super User (Alumni)

Re: application scripting... Group by , variable

Jim is correct. 

Without Names Default To Here(1), all variables are global. The JMP Scripting Guide states

"When a global variable and a column have the same name, ... In this situation, you must scope the column name."

In fact, when I run the script, I get the error 

            Cannot set value for the column 'age' because the row number (-1) is not valid.

 

Since it is not ::age, JMP thinks the statement age=13 is trying to assign values to the column named age.  Names Default To Here(1) makes the variable "age" local and resolves this conflict. However,  I strongly recommend that you use Names Default to Here(1)  and use a different name for user input like, _age or usr_val.  I believe name collisions (conflicts, a.k.a. scoping) is the second highest occuring scripting error after syntax and typos. 

 

The script below is a slight modification of Jim's:

  • _age is assigned a value
  • an alternative to Fit Where().  Selecting the values to be used and then using Group By() will provide the name for each Group By value. 
names default to here(1);

dt=open("$SAMPLE_DATA/big class.jmp");

_age=13;

biv =  dt << Bivariate(
	Y( :weight),
	X( :height),
	Show Points( 0 ),
	where(:age==12 | :age==_age),
	Group By(:age),
	Fit Each Value( {Report(0)}),
	By(:sex)
);

biv << ( Curve[1] << Line Color ( {0, 0, 255    } ) );
biv << ( Curve[2] << Line Color ( {213, 72, 255 } ) );


View solution in original post

3 REPLIES 3
txnelson
Super User

Re: application scripting... Group by , variable

The issue appears to be a scoping problem.  I was not able to run your script successfully until I added

Names Default to Here( 1 );

Here is your script with the column names pointing to the Big Class data table.

names default to here(1);

dt=open("$SAMPLE_DATA/big class.jmp");

age=13;

Bivariate(
Y( :weight),
X( :height),
Show Points( 0 ),
Fit Where(
:age == 12,
Fit Each Value( {Report( 0 ), Line Color( {0, 0, 255} ), Report( 0 )} )
),
Fit Where(
:age == age,  //this is troublesome line... the graph is generated but not quite right.
Fit Each Value( {Report( 0 ), Line Color( {213, 72, 87} ), Report( 0 )} )
),
By( :sex)
);
Jim
gzmorgan0
Super User (Alumni)

Re: application scripting... Group by , variable

Jim is correct. 

Without Names Default To Here(1), all variables are global. The JMP Scripting Guide states

"When a global variable and a column have the same name, ... In this situation, you must scope the column name."

In fact, when I run the script, I get the error 

            Cannot set value for the column 'age' because the row number (-1) is not valid.

 

Since it is not ::age, JMP thinks the statement age=13 is trying to assign values to the column named age.  Names Default To Here(1) makes the variable "age" local and resolves this conflict. However,  I strongly recommend that you use Names Default to Here(1)  and use a different name for user input like, _age or usr_val.  I believe name collisions (conflicts, a.k.a. scoping) is the second highest occuring scripting error after syntax and typos. 

 

The script below is a slight modification of Jim's:

  • _age is assigned a value
  • an alternative to Fit Where().  Selecting the values to be used and then using Group By() will provide the name for each Group By value. 
names default to here(1);

dt=open("$SAMPLE_DATA/big class.jmp");

_age=13;

biv =  dt << Bivariate(
	Y( :weight),
	X( :height),
	Show Points( 0 ),
	where(:age==12 | :age==_age),
	Group By(:age),
	Fit Each Value( {Report(0)}),
	By(:sex)
);

biv << ( Curve[1] << Line Color ( {0, 0, 255    } ) );
biv << ( Curve[2] << Line Color ( {213, 72, 255 } ) );


pjr1121
Level II

Re: application scripting... Group by , variable

Thank you very much for the quick responses. i forgot to copy it into my example but the very beginning of my script started with "names default to here(1);"  Don' think it matters where that command apperas within the script, so long as it is before the relevent function.

 

I tried Jim's code first and it produced the same problem as my original script.  The value 13 was not dispayed below the graph, but instead "age==age".  note: i'm running JMP 12

Capture.PNG

As i'm using a variable name which does not match a column in my tables, i don't think i'm running into the issue of JMP trying to assign the column values as mentioned by gzmorgan0.

 

The approach of using 'group by' instead of 'fit where' did the trick.  Since I copied this script from a bivariate created via "group by" menu options, i'm not confused as to why it used 'fit where' instead.  But that's another problem for another day.  Thanks for the hel .