We’re asking you to select a content label when starting a new topic in the Discussions area. Read more to find out why.
Choose Language Hide Translation Bar
Highlighted
robhanssen
Level III

Table scripts?

It could definitely be a case of RTFM, but I have a question:

 

In one of my JMP datasets, I use a function as intermediate in multiple formula columns. To prevent duplication of the same code (and duplication of work if I would have to change that function), I put the function in a script and saved it to the data table.

 

The problem comes when I open up the dataset and want to add data. In order for the formula columns to run correctly, I first have to hit the green arrow next to the script to run it first before the formula columns can do their job. If the script is not run, the function is not defined.

 

Is there a way to enact functions like these in form similar to the Table Variables, which are automatically run when opening the data file?

 

edit: I attached an example file. The script is in the Utility Functions script.

2 REPLIES 2
Highlighted
Craige_Hales
Staff (Retired)

Re: Table scripts?

See if the OnOpen script meets your needs; it will (by default) prompt. You can change the preference if you need to. Note that the OnOpen script does NOT run when the following script is run, but if you save the empty table, close it, and open it again, the script will run. Then adding rows to the freshly opened table will be able to use the global function.

 

New Table( "globalFunctionOnOpen",
	Add Rows( 0 ),
	New Script( "OnOpen", global:myFunc = Function( {a, b}, a + b ) ),
	New Column( "x", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
	New Column( "y", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [] ) ),
	New Column( "z", Numeric, "Continuous", Format( "Best", 12 ), Formula( global:myfunc( :x, :y ) ), Set Selected )
)
Craige
Highlighted
gzmorgan0
Super User

Re: Table scripts?

@robhanssen

Below are a couple options to consider. What might be the best choice depends upon usage.

  1. Refactor: For this example, the simple utility function can be achieved with a column I'll call MTD.  The function returns a 1 or 0. Then change your functions : change  yearToDate(:month, :Elec Cost)  to be :MTD * :ElecCost.  In other words, use column functions, when possible.
  2.  Make an Add-in: something that is specific for the table, called UpdateMyTablename, or a script that runs all your utility functions, called RunUtiltyFunc. Note an Add-in can be shared and used by other users and run on demand, and the functions wil be avaiable the entire sessions unless globals are deleted.
  3. Create a Start-up Script: If you have a set of utility functions that you might use often, you can define/include/run them via a start-up script. Examples I have used include: a corporate manufacturing specific shift definition, workweek function, logo or commonly used math function. From the JMP Scripting Guide > Creating Applications > Add-Ins

Specify Start-up and Exit Scripts (Optional)
Click the Start-Up Script tab to add a script that runs when JMP starts up (and the add-in
starts). You can select an existing script (Run JSL in this file) or copy and paste in a script (Run
this JSL). For example, you could provide a message telling the user that the add-in is
installed upon start-up.