Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Re: make NESTED custom functions usable in formulas

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Aug 10, 2018 7:29 AM
(2978 views)

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:

but I get the following error:

Is there a way to do this?

1 ACCEPTED SOLUTION

Accepted Solutions

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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:

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

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.