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

How to know if a table is a linked subset?

Hello everybody,

 

I would like to determine (using JSL) if the current data table is linked to a main table or not.

I tried "<< Get Table Variable Names" but it didn't work.

Do you have any idea of how we can get this information?

 

Thanks in advance for your help!

3 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales
Super User

Re: How to know if a table is a linked subset?

you might be able to use the table variable; it will need some testing because I'm not sure if it is always set (update: tried several ways to make a subset and it seems the table variable is always created): 

dt<<gettablevariable("Linked Subset")
"This subset is linked to Big Class"

// but not linked parent table:
Data Table( "Big Class" )
dt<<gettablevariable("Linked Subset")
""

This linked subset has a table variableThis linked subset has a table variable

Craige

View solution in original post

ian_jmp
Staff

Re: How to know if a table is a linked subset?

You could test this (along the lines I mentioned earlier):

NamesDefaultToHere(1);

// Function to determine if two tables are linked
rLinked = Function({dt1, dt2}, {Default Local},
	dt1 << selectAllRows;
	if(NRow(dt2 << getSelectedRows) > 0, linked = 1, linked = 0);
	dt1 << clearSelect;
	dt2 << clearSelect;
	linked;
);

// Look at the open tables in the session and see which pairs are linked
linkedTables = Function({}, {Default Local},
	nt = NTable();
	linkMat = J(nt, nt, .);
	tblNames = {};
	for(t1=1, t1<=nt, t1++,
		InsertInto(tblNames, DataTable(t1) << getName);
		for(t2=t1+1, t2<=nt, t2++,
			if(rLinked(DataTable(t1), DataTable(t2)), linkMat[t1, t2] = 1, linkMat[t1, t2] = 0);
			);
		);
	EvalList({linkMat, tblNames});
);

// Make some active tables
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
// Unlinked subset
dt << selectRandomly(0.1);
dt2 = dt << Subset(LinkToOriginalDataTable( 0 ) );
dt2 << setName("Unlinked");
// Linked subset
dt << selectRandomly(0.1);
dt3 = dt << Subset(LinkToOriginalDataTable( 1 ) );
dt3 << setName("Linked");

// Get the 'link matrix'
linkedOrNot = linkedTables();
dt4 = AsTable(linkedOrNot[1], << setName("Linked Tables"), << columnNames(linkedOrNot[2]));
dt4 << NewColumn("Column", Character, Values(linkedOrNot[2]));
dt4 << moveSelectedColumns({"Column"}, ToFirst);

View solution in original post

ian_jmp
Staff

Re: How to know if a table is a linked subset?

Untested (as usual . . . :(

NamesDefaultToHere(1);
ClearLog();

// See if a table is a summary of another table
isSummary = 
Function({dt}, {Default Local},
	ts = dt << Get Table Script Names;
	if (Contains(ts, "Source"),
		ss = dt << getProperty("Source");
		if (Head(Arg(ss, 2)) == Expr(Summary), 1, 0),
		0
		);
	);

// Make a summary table
dt1 = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt2 = dt1 << Summary(Mean( :height ));
Print(isSummary(dt1));
Print(isSummary(dt2));

View solution in original post

23 REPLIES 23
ian_jmp
Staff

Re: How to know if a table is a linked subset?

FWIW, assuming your code makes the tables, you can make linked or unlinked subsets as you require:

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
// Unlinked subset
dt << selectRandomly(0.1);
dt2 = dt << Subset(LinkToOriginalDataTable( 0 ) );
dt2 << setName("Unlinked");
// Linked subset
dt << selectRandomly(0.1);
dt3 = dt << Subset(LinkToOriginalDataTable( 1 ) );
dt3 << setName("Linked");
anne_sa
Level VI

Re: How to know if a table is a linked subset?

Thank you for your answer Ian.

However my objective is not to create a linked subset but to determine if an existing table is linked to another or not.
Sorry if I was not clear enough!

ian_jmp
Staff

Re: How to know if a table is a linked subset?

No, I understood, but was implying that you could avoid the 'problem'. But if you want to tell retrospectively, you could do something like:

Names Default To Here( 1 );

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
// Unlinked subset
dt << selectRandomly(0.1);
dt2 = dt << Subset(LinkToOriginalDataTable( 0 ) );
dt2 << setName("Unlinked");
// Linked subset
dt << selectRandomly(0.1);
dt3 = dt << Subset(LinkToOriginalDataTable( 1 ) );
dt3 << setName("Linked");


// Function to determine if two tables are linked
rLinked = Function({dt1, dt2}, {Default Local},
	dt1 << selectAllRows;
	if(NRow(dt2 << getSelectedRows) > 0,
		linked = 1,
		linked = 0
		);
	dt1 << clearSelect;
	linked;
);

Print(rLinked(dt, dt2), rLinked(dt, dt3));
anne_sa
Level VI

Re: How to know if a table is a linked subset?

Thanks Ian, that is indeed a quite convenient way to solve the problem!

I just need to go one step further. How can we do that if we do not know what is the potential main table (dt in your code)?

Do we have to test all opening tables or is there a faster solution?

 

 

Craige_Hales
Super User

Re: How to know if a table is a linked subset?

you might be able to use the table variable; it will need some testing because I'm not sure if it is always set (update: tried several ways to make a subset and it seems the table variable is always created): 

dt<<gettablevariable("Linked Subset")
"This subset is linked to Big Class"

// but not linked parent table:
Data Table( "Big Class" )
dt<<gettablevariable("Linked Subset")
""

This linked subset has a table variableThis linked subset has a table variable

Craige
anne_sa
Level VI

Re: How to know if a table is a linked subset?

Thanks for the suggestion Craige!

I tried "Get Table Variable Names" but not "Get Table Variable"!

 

I did some tests and it seems to work pretty well!

So far, there is just one particular case where it doesn't work. If I create a summary table linked to the original table, there is no table variable created.

 

Any idea for this last point?

ian_jmp
Staff

Re: How to know if a table is a linked subset?

Yes, the 'summary table' scenario is the one I tried - Hence my replies above. But I'm still left musing over how your JMP session can get into a state where you don't know what's linked to what. But that's OK.

 

You could easily beuild a 'linking matrix' using 'NTable()' and what I've given above.

Craige_Hales
Super User

Re: How to know if a table is a linked subset?

I'm going with Ian's answer then.

 

Why do you need the linked table information? Maybe there is another approach. 

Craige
anne_sa
Level VI

Re: How to know if a table is a linked subset?

Actually I am using a script which runs several analyses. In particular, it saves the residuals and other columns from the Fit Model platform, in the current data table. If this table is linked to another one, columns are saved in the original table and not in the current one and it makes fail my script.

So when a user launches the script, I would like to check first if the current table is not linked to another one. And since I am not aware of what the user did before, that is the reason why I am not able to know what could be linked to what.

Let me know if you need more explanations or if something is not clear!

 

Besides to answer Ian's last post, I see how we could build this linking matrix but if we have a summary table, I am not sure that the previous script will work. If I am not wrong when you select rows in the original table, nothing is selected in the summary table.

So unfortunately this case is still problematic.