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

make NESTED custom functions usable in formulas

Hi all,

I am trying to build an add to make some complex calculations based on temperature and pressure.

my approach is making a script and then attach this to an invisible add-in. Then use use the formulas in the pluging, in Formulas via global:myfunc().

 

If I have a single function on my script this approach works (see topic).

However, if I have two nested functions in one jsl script. This approach does not work anymore.

Here is my jsl script:

myfun1 = Function({x},

a = x+10;

Return(a);

);

myfun2 = Function({x},

b = myfun1(x)+10;

Return(b);

);

So what I would like to do in practice is call myfun2 from the Formula editor as follow:

fun.jpg

 

but I get the following error:

Scripting error.jpg

Is there a way to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: make NESTED custom functions usable in formulas

My suggestion, if you are not using JMP 14 would be to create all of your functions in a separate name space.  That will make the functionsa available to all data tables and scripts.  You could also structure you addin to actually modify the jmpstartup.jsl script to create the namespace and to populate it with all of the functions, therefore maing the functions automatically available whenever someone starts up JMP.

names default to here(1);
GlobalFunction=new namespace("GlobalFunction");
GlobalFunction:ShowValue = Function({x},show(x));

zippy=10;
GlobalFunction:ShowValue(zippy)
Jim

View solution in original post

4 REPLIES 4
cwillden
Super User (Alumni)

Re: make NESTED custom functions usable in formulas

I would be really careful with using custom user-defined functions in a column formula because once you close JMP and then open that table again, those column formulas are going to break since those function names will be undefined.  Maybe that's not a concern here, but just thought I would point that out.


There's quite a few ways to approach this.  You could take an approach using expressions, for example.  Or you could put the definitions of myfun1 and myfun2 right into the column formula:Capture.PNG

 

 

Which is just pasting this into the formula:

myfun1 = Function( {x},
	a = x + 10;
	Return( a );
);
myfun2 = Function( {x},
	b = myfun1( x ) + 10;
	Return( b );
);
myfun2( 1 );
-- Cameron Willden
Mattia
Level III

Re: make NESTED custom functions usable in formulas

Hi, thanks for the reply.

 

Well the problem is a bit more complicated than the example script in attachment. The script that I really want to make consists of 30+ functions and some of them call up several others.

The idea is that this script is made avaiable to other people via a JMP add-in. So If the add-in is installed they can access the functions at any time from any table.

I basically want to have the equivalent of a python/C++ class with functions in it that can be called up.

 

What I want to know is: how do i make it work from the column formula?

If I call myfun2 within the script, that does not give me an error. But I have it when running it from the column formula.

 

I also know that in JMP14 is officially supported to introduced own costum formulas (link).

I am pretty sure this should be possible, it is just a matter on how to do it properly in jsl/JMP and that is exactly my question.

txnelson
Super User

Re: make NESTED custom functions usable in formulas

My suggestion, if you are not using JMP 14 would be to create all of your functions in a separate name space.  That will make the functionsa available to all data tables and scripts.  You could also structure you addin to actually modify the jmpstartup.jsl script to create the namespace and to populate it with all of the functions, therefore maing the functions automatically available whenever someone starts up JMP.

names default to here(1);
GlobalFunction=new namespace("GlobalFunction");
GlobalFunction:ShowValue = Function({x},show(x));

zippy=10;
GlobalFunction:ShowValue(zippy)
Jim
Mattia
Level III

Re: make NESTED custom functions usable in formulas

Thanks for the tip, it works like a charm now! :)

I thought that: 

names default to here(1);

 was goign to be be enough in this case. I now understand why I need to clearly specify a namespace.