cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
hogi
Level XII

Custom Function - how to reference the column

It is cool that custom functions can be used as well as part of custom formulas in Formula Editor.

 

With the Transform Category command, the 

New Custom Function( "myNamespace", "Add Ten", Function( {x, y = 10}, x + y ),  << Transform Category( "Custom" ));

from https://www.jmp.com/support/help/en/16.2/#page/jmp/create-custom-functions-transforms-and-formats.sh...

will show up in the right click New Formula Column Menu - and produce a column with formula

hogi_0-1698521852106.png

 

I just started with @txnelson 's approach 
https://community.jmp.com/t5/Discussions/Formula-for-Number-of-Unique-Categories-in-Column/m-p/55284... 
to generate a column to  Count Unique values - but instead of typing the formula, I wanted to use a Custom Function.


The manually generated Formula Column works, but the column that uses the Custom Function count unique doesn't.

 

It fails with the error message

hogi_1-1698522649974.png

Seems that x in the by() expression is not replaced with the column name.

So, use Eval(Substitute() to fix the issue?
... but how?

With

Eval(Substitute(Expr(Summarize( values = by( _col_ ) )),Expr(_col_),Name Expr(x) ));

I get

hogi_3-1698523534416.png

- because in a Column Formula :x is treated as :x[row()]. Argh ...

 

Along this line, I start to wonder why does this work:
hogi_2-1698523113734.png

[NB: the red zig-zag line just tells the user that print in a column formula is not a good idea]


 why does Jmp know that it has to send get values to the COLUMN :sex and and not to :sex[row()].
And why does it actually return :sex[row()] in the second line - and not the column :sex ?

 

 

 

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

Add Custom Functions(
	New Custom Function(
		"custom",
		"count unique",
		Function( {x},
			As Constant( Summarize( values = by( x ) ) );
			N Items( values );
		),
		<<Transform Category( "Custom" ),
		<<Description( "count unique values" )
	)
);

New Column( "functionColumn",
	Formula(
		As Constant( Summarize( values = by( :sex ) ) );
		N Items( values );
	)
); //works

New Column( "customFunctionColumn", Formula( custom:count unique( :sex ) ) ); //doesn't work

 

 

 

4 REPLIES 4

Re: Custom Function - how to reference the column

Hello @hogi ,

I'm not sure if this is a smart approach, but the following script works. I used eval insert function. I hope it helps.

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

Add Custom Functions(
	New Custom Function(
		"custom",
		"count unique",
		Function( {x},
			Eval( Parse( Eval Insert( "As Constant( Summarize( values = by( :^x^ ) ) )" ) ) );
			N Items( values );
		),
		<<Transform Category( "Custom" ),
		<<Description( "count unique values" )
	)
);

New Column( "customFunctionColumn", Formula( custom:count unique( "sex" ) ) ); 
hogi
Level XII

Re: Custom Function - how to reference the column

hm, unfortunately, it doesn't work:

hogi_1-1698956749394.png

produces the error message:

hogi_0-1698956713087.png

 

So, the same as like with

Substitute (..., Expr(_col_),Name Expr(x))

 

I guess it's because a function call to make it work has to be  

custom:count unique( "sex" )

instead if 

custom:count unique( :sex )
hogi
Level XII

Re: Custom Function - how to reference the column

I just found an earlier (2018) version of the same question:

Column reference in custom function 

 

hogi
Level XII

Re: Custom Function - how to reference the column

The quite definite reply from Jmp Support TS-00063463

 

hogi_1-1699302157298.png