cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
FN
FN
Level VI

Column() vs As Column() vs datable:column vs dt:As name("column")

JSL has several ways to refer to columns. Let's open a data table first.

 

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Bands data.jmp" );

Now we create a table script that has something like this.

dt = current data table();

print(dt:timestamp == Column(dt, "timestamp")); 
// 0

print(Column(dt, "timestamp")==As Column(dt, "timestamp")); 
//0

In automated scripts (for example, a graph), when a user changes the name of a column, it seems to adapt the script to the new column name. This failed in custom JSL, with something like Index() appearing.

 

How one would use all of these methods to make a JSL script robust to column name changes?

 

Regards,

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

This is a bit long post and jumping here and there, sorry about that..

 

As Column(), Column(), :colname, ...  isn't simple in JSL and which to use depends on the case. My preferred method is to always use (except when I cannot or if I want a bit cleaner look in script) Column(datatable_reference, "string of the name of the column"). :colname does have it's benefits which can be sometimes used for scripters benefit (some are mentioned here Scripting Guide > JSL Building Blocks > Rules for Name Resolution > Rules for Resolving Names in Exceptions section).

 

I also like to use strings of column names instead of :colname syntax to avoid some issues that might happen here and there. For example << Get Column Names("String") instead of just << Get Column Names() and then I get the reference with Column(string) if needed.

 

Column(<dt>, "name", "formatted")  and As Column(name) are not same. Column() returns you a reference to column and As Column "accesses" a column (what this means depends on the case...). When you first run this script dt:timestamp will return missing value, as the Row() hasn't been properly defined for JMP's indexing (it is initialized as 0).

 

 

View more...
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
//Row() = 0;
Show(Row(), dt:name, Column(dt, "name"), As Column(dt, "name"));
Row() = 1;
Show(Row(), dt:name, Column(dt, "name"), As Column(dt, "name"), Column(dt, "name")[], Column(dt, "name")[Row()]);

Print(dt:name == Column(dt, "name")); 
// 0

Print(Column(dt, "name") == As Column(dt, "name")); 
//0

Print(dt:name == As Column(dt, "name"));
//1

stop();

// 
Show(Column("name") << get values);
Show(As Column("name") << get values);
Show(:name << get values);

stop();

col1 = "name";
gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(col1), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(Eval(col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(As Column(col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(Column(col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(Column(dt, col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

 

 

@ih has some good suggestions on how to manage name changes in columns. Also when inserting columns to for example Graph Builder, you can use Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute and sometimes Eval().  << Get Name can also be helpful from time to time or building user interface with Filter Col Selector / Col List Box for example (like @pauldeen suggested).

 

-Jarmo

View solution in original post

13 REPLIES 13
pauldeen
Level VI

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

Either index the column by numer column(dt, 1) or prompt the user to select a column or enter a column name. Look into New Window() in the help files.

ih
Super User (Alumni) ih
Super User (Alumni)

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

How would you know which column the script should reference?  If you really need to monitor for column name changes you could do that by subscribing to changes in the table and recording those column names somewhere, but I think that would be pretty complex.  Could you instead define a column property and search for that?

 

Subscribe example from Scripting Guide:

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
f = Function( {dtab, col, oldname},
	Print( dtab << getname() );
	Print( "new column name", (col << getname()) );
	Print( "old name", oldname );
);
sub = dt << Subscribe( "", OnRenameColumn( f ) );
Column( dt, 1 ) << set name( "test" );
Wait( 1 );
dt << unsubscribe( sub, on rename column );

Use column property

Names Default To Here( 1 );

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

dt:height << Set Property( "My Script Identifier", X );

XVar = Filter Each({c}, dt << Get Column References,
	ident = char(c << Get Property( "My Script Identifier" ));
	if( is empty(ident), 0, ident == "X", 1, 0)
)[1];
hogi
Level XII

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

you mean something like this?

 

After running the code, the first two entries in the TABLE SCRIPT were automatically changed to the new name *)


Same behavior for any graph etc. : everything gets automatically adjusted to the new column name.
so cool!


But for As column() and Column() it didn't work.

Neither did it for any column name in a script that is NOT stored as a table script ...

 

 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Script(
	"script",
	:name;
	:"name"n; // TS-00076655
As Column( "name" ); Column("name"); ); dt:name << Set Name( "name_2" );

 

hogi
Level XII

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

Amazing that a Dashboard still works after renaming columns

- if it's stored as a Table Script

 

edit: 
ah, no ... it even works if the Dashboard/Application is NOT stored as a Table Script.
Interesting.

 

edit2 - limitation:

the manually created JSL "Scripts" of the application don't get updated automatically, just the code in the background of the application.

hogi_0-1700510893640.png

 

 

hogi
Level XII

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

Here is an example where a table script doesn't get updated correctly:


:Age is renamed to :AGE. Seems that Jmp doesn't expect a difference - and doesn't replace the column reference in the table script with the new name.

 

So, at the end, in the table script the column is still :Age  - and in the table there are columns :age and :AGE.

... and Jmp picks :age.

 

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

new column ("Age",set each value(2));

dt << Add Properties to Table(
	{New Script(
		"summary",
		Data Table( "Big Class" ) << Summary(
			Group( :Age ),
			Freq( "None" ),
			Weight( "None" ),
			output table name( "Summary of Big Class grouped by age" )
		)
	)}
);

dt << run script("summary"); // Age = 2
:Age << set name("AGE");
dt << run script("summary"); // age = 13 - 17

 

hogi
Level XII

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

 @brady_brady  discusses this topic in detail in Better JSL Smoother Migration: How To Fix Your Own Code​  @10min

 

Conny_Wang
Level II

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

因为你的列名称是数字,如果纯英文字母不会出现这样的情况

jthi
Super User

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

This is a bit long post and jumping here and there, sorry about that..

 

As Column(), Column(), :colname, ...  isn't simple in JSL and which to use depends on the case. My preferred method is to always use (except when I cannot or if I want a bit cleaner look in script) Column(datatable_reference, "string of the name of the column"). :colname does have it's benefits which can be sometimes used for scripters benefit (some are mentioned here Scripting Guide > JSL Building Blocks > Rules for Name Resolution > Rules for Resolving Names in Exceptions section).

 

I also like to use strings of column names instead of :colname syntax to avoid some issues that might happen here and there. For example << Get Column Names("String") instead of just << Get Column Names() and then I get the reference with Column(string) if needed.

 

Column(<dt>, "name", "formatted")  and As Column(name) are not same. Column() returns you a reference to column and As Column "accesses" a column (what this means depends on the case...). When you first run this script dt:timestamp will return missing value, as the Row() hasn't been properly defined for JMP's indexing (it is initialized as 0).

 

 

View more...
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
//Row() = 0;
Show(Row(), dt:name, Column(dt, "name"), As Column(dt, "name"));
Row() = 1;
Show(Row(), dt:name, Column(dt, "name"), As Column(dt, "name"), Column(dt, "name")[], Column(dt, "name")[Row()]);

Print(dt:name == Column(dt, "name")); 
// 0

Print(Column(dt, "name") == As Column(dt, "name")); 
//0

Print(dt:name == As Column(dt, "name"));
//1

stop();

// 
Show(Column("name") << get values);
Show(As Column("name") << get values);
Show(:name << get values);

stop();

col1 = "name";
gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(col1), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(Eval(col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(As Column(col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(Column(col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

gb = dt << Graph Builder(
	Size(529, 466),
	Show Control Panel(0),
	Variables(X(Column(dt, col1)), Y(:height)),
	Elements(Points(X, Y, Legend(6)))
);

 

 

@ih has some good suggestions on how to manage name changes in columns. Also when inserting columns to for example Graph Builder, you can use Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute and sometimes Eval().  << Get Name can also be helpful from time to time or building user interface with Filter Col Selector / Col List Box for example (like @pauldeen suggested).

 

-Jarmo
vince_faller
Super User (Alumni)

Re: Column() vs As Column() vs datable:column vs dt:As name("column")

I don't usually like +1 posts, but @jthi this is the exact same methodology I do for production scripts.  I usually think about it like

 

Names default to here( 1 );
dt = open("$SAMPLE_DATA\Big Class.jmp");
col_name = "age";
show(ismissing(AsColumn(dt, col_name)) & ismissing(dt:age)); // these are both the same as far as I know
cols = dt << get column references; // these are the actual references
show(cols[2] == Column(dt, col_name)); // these are references

 

Vince Faller - Predictum