If you want to receive a message when a data table changes, use the Subscribe message. For example, you might want a message sent to the log when columns are added or deleted.
Here is the basic syntax:
dt << Subscribe( keyname(<client>), On Delete Columns|On Add Columns|On Add Rows|On Delete Rows|On Rename Column|On Close|On Save|On Rename(function) );
The first argument is a name for the subscription (or “client”), so that it can also be removed.
Note: The subscription is to the data table. The subscriber subscribes to the broadcast from the data table; it does not subscribe to a program or to script.
The application can also subscribe as a client to the data table (for example, most built-in platforms such as Distribution). If a data table has clients when the data table is closed, the user is warned that there are applications open that might need the data table.
Each subscription remains in effect until you unsubscribe:
dt << Unsubscribe("keyname", On Delete Columns | On Add Columns | On Add Rows | On Delete Rows | On Close | On Col Rename | All);
The following example sends messages to the log when a row is added or deleted:
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
delRowsFn = Function( {a, b, rows},
dtname = ( a << Get Name());
Print( dtname );
Print( b );
Print Matrix( rows );
);
addRowsFn = Function( {a, b, insert},
dtname = ( a << Get Name() );
Print( dtname );
Print( b );
Print( insert );
);
dt << Subscribe( "Test Delete", onDeleteRows( delRowsFn, 3 ) );
dt << Subscribe( "Test Add", onAddRows( addRowsFn, 3 ) );
Subscribe to Data Table List() subscribes to a list of data tables to be notified when a new data table has been added, closed, or renamed. The following example shows how to subscribe and unsubscribe:
f2 = Function( {x}, Show( x ) );
Show( Subscribe to Data Table List( "My Data", OnClose( f2 ) ) );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
Wait( 2 );
Close( dt );
Show( Unsubscribe to Data Table List( "My Data" ) );
Subscribe to Data Table List( "My Data", OnClose( f2 ) ) = "My Data";
x = Data Table( "Big Class" );
Unsubscribe to Data Table List( "My Data" ) = Empty();
If Subscribe is called with an empty application name, JMP generates a unique name that is returned to the caller. In the following example, appname2 is subscribed to the data table as a client.
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
appname1 = dt << Subscribe( "", On Close( Print( "Closing Data Table" ) ) );
appname2 = dt << Subscribe(
""( "client" ),
On Close(
Function( {dtab},
dtname = ( dtab << Get Name() );
Print( dtname );
)
)
);
dt << Unsubscribe( appname1, On Close );
dt << Run Script( "Distribution" )