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
hogi
Level XII

ThisDataTable() ?

For Columns and Objects like Reports, there exists  a command

obj << Get Data Table();

to get a reference to the respective data table.

 

Is there a similar function for Table Scripts? something like 

This Data Table()

 

some lazy implementations which could lead to issues:

  • reference the data table by its name.
    Data Table ("my table") << new column("test")
    this works till there is a conflict with another data table with the same name and " 2" is added automatically to the table name.
  • use mydt = current data table() to generate a reference to the data table
    This works fine if I run the script from the table itself. To have the chance to click on the play button, one has to select the data table. Therefore, it will be the current one. good.
    But when I trigger the Table Script via dt << run sricpt ("myscript"), another data table could be the current one.
    edit #1: @jthi's example below suggests, that this statement is not correct:
    When you trigger a table script, the table with the table script gets the current data table.
    so: no issues.
    edit #2: ... but it seems that "gets the current data table" doesn't happen immediately
    so: under some circumstances there could be some issues (see below)
1 ACCEPTED SOLUTION

Accepted Solutions
peng_liu
Staff

Re: ThisDataTable() ?

It sounds a little convoluted, but

  1. I believe that a script in a data table implicitly sends messages to the data table that encloses the script. So explicitly referring to the data table using "current data table" is unnecessary.
  2. If you run a script inside of a data table from out side of the data table, you must obtain the handle of the data table first, such that you can send Run Script("script name") to the data table. The data table can be the current or not, which does not matter.

So, I guess that the answer is that there is no need to use anything, in a script, to refer to the data table that encloses the script. Remove "dt = current data table()" from your script and see how it goes. I guess there are some other reasons why you need that line, but maybe there is a different solution to address that problem.

View solution in original post

5 REPLIES 5
peng_liu
Staff

Re: ThisDataTable() ?

It sounds a little convoluted, but

  1. I believe that a script in a data table implicitly sends messages to the data table that encloses the script. So explicitly referring to the data table using "current data table" is unnecessary.
  2. If you run a script inside of a data table from out side of the data table, you must obtain the handle of the data table first, such that you can send Run Script("script name") to the data table. The data table can be the current or not, which does not matter.

So, I guess that the answer is that there is no need to use anything, in a script, to refer to the data table that encloses the script. Remove "dt = current data table()" from your script and see how it goes. I guess there are some other reasons why you need that line, but maybe there is a different solution to address that problem.

hogi
Level XII

Re: ThisDataTable() ?


@peng_liu wrote:
  1. I believe that a script in a data table implicitly sends messages to the data table that encloses the script.

Thanks. interesting, good to now.

To be honest, a bit of a freaky feeling to remove all those

dt <<

  They gave me some peace of mind that the script was doing exactly what it was supposed to do. I believe it works without ...

Even 

Nrows(dt);

seems to work.

 

jthi
Super User

Re: ThisDataTable() ?

dt = current data table() in the table script:
This works fine if I run the script from the table itself. To have the chance to click on the play button, one has to select the data table. Therefore, it will be the current one. good.
But when I trigger the Table Script via JSL, another data table could be the current one.

Have you verified this to be the case if you run it via JSL? If I remember correctly I have been running some of my more complicated table scripts just fine through JSL, without needing to worry about correcting the current data table (I might remember wrong though).

View more...
Names Default To Here(1);

dt3 = New Table("table with script",
	Add Rows(2),
	Compress File When Saved(1),
	New Script(
		"test", JSL Quote(Local Here(
dt = Current Data Table();
dt << Graph Builder(
	Size(528, 454),
	Show Control Panel(0),
	Variables(X(:Column 1), Y(:Column 2)),
	Elements(Points(X, Y, Legend(3)), Smoother(X, Y, Legend(4)))
);
show(dt);
);
)	,
		As String(1)
	),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([2, 3]))
);

dt2 = New Table("current table",
	Add Rows(0),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([]))
);

Current Data Table(dt2);
Wait(1);
Show(Current Data Table());
dt3 << Run Script("test");
Show(Current Data Table());

-Jarmo
hogi
Level XII

Re: ThisDataTable() ?

Looks convincing

So, when running a table script, the current data table is automatically mapped to the table with the script.
So, this is the reason why a script in a data table [...] sends messages to the data table that encloses the script.

 

automatically: yes

immediately: no

see below:

hogi
Level XII

Re: ThisDataTable() ?

Hi,

 

I could replicate my error from 2023 and have new findings:

 

The problem:

  • a jsl script triggers dt << run script(myScript)
  • myscript starts with
    mydt = current data table();
  • my original assumption: when I trigger the Table Script via JSL, another data table could be the current one.
    seems to hold for some cases -  today I could generate such a case where
mydt = current data table();
mydt << new column ("test1"); // was sent to the wrong data table
new column("test2"); // was sent to THIS DATA TABLE (fits to Peng Liu's post)

 

So:
If you plan to run a Table script via dt << run script(myScript)

and need a reference to ThisDataTable, add wait(0) to be on the safe side:

wait(0); // !!!!
thisDataTable = current data table();


similar findings, but with slightly different context : run table script