Better put everything into As Constant and stop the "formula" before the main part begins.
... super fast.
... with the help of a small GUI: quite flexible
... and via the main command that is allowed to sleep forever: even dynamic : )
Names Default To Here( 1 );
Try(
dt = Current Data Table(),
Caption( "no dat table" );
Stop();
);
updateLBox = Function( {t},
(t << sib) << setitems( (t << topparent)[listboxbox( 1 )] << getselected )
);
nw = New Window( "rank unique entries",
<<Type( "Modal Dialog" ),
<<return result,
Border Box( Left( 10 ), Right( 10 ), bottom( 5 ),
V List Box(
Lineup Box( N Col( 1 ), spacing( 10 ),
// Text Box( "XG Boost Specification" ),
// colistbox only necessary if the script is standalone
H List Box(
Panel Box( "Select Columns", Col List Box( all, nlines( 15 ) ) ),
Panel Box( "Cast Selected Columns into Roles",
Lineup Box( N Col( 2 ),
Button Box( "Unique", <<setfunction( Function( {t}, updateLBox( t ) ) ) ),
Cunique = Col List Box( nlines( 2 ), minitems( 1 ), maxitems( 1 ) ),
Button Box( "By Group", <<setfunction( Function( {t}, updateLBox( t ) ) ) ),
CBy = Col List Box( nlines( 2 ), minitems( 0 ) )
)
)
)
),
Spacer Box( size( 1, 10 ) ),
Button Box( "OK",
<<setfunction(
Function( {t},
ColsUnique = Cunique << get items;
ColsBy = Cby << get items;
)
)
),
Spacer Box( size( 1, 10 ) ),
)
)
);
If( Not( nw["Button"] == 1 ),
Stop()
);
myCol = dt << New Column( "Rank_" || Char( ColsUnique ) || If( N Items( ColsBy ), "_by_ " || Char( ColsBy ), "" ) );
colName = mycol << get name();
groupExpr = Expr( Group() );
For Each( {col}, ColsUnique, Insert Into( groupExpr, Name Expr( As Column( col ) ) ) );
For Each( {col}, ColsBy, Insert Into( groupExpr, Name Expr( As Column( col ) ) ) );
mainCommand = Substitute( Name Expr( groupExpr ), Expr( Group() ), Expr( Concat Items() ) );
If( N Items( ColsUnique ) == 1,
columnsExpr = Eval Expr( Columns( Expr( Name Expr( As Column( ColsUnique[1] ) ) ) ) ),
Caption( "multiple columns not implemented yet" );
Stop();
);
GroupByExpr = Expr( Group By() );
For Each( {col}, ColsBy, Insert Into( groupByExpr, Name Expr( As Column( col ) ) ) );
Eval(
Eval Expr(
myCol << set formula(
As Constant(
dt0 = Current Data Table();
dtsum = dt0 << Summary( Expr( Name Expr( groupExpr ) ) );
newCol = dtsum << New Formula Column(
Operation( Category( "Distributional" ), "Rank" ),
Expr(
Name Expr( Columns Expr )
),
Expr(
Name Expr( GroupByExpr )
),
);
newcol[1] << set name( Expr( colName ) );
dtsum << run formulas();
dt0 << Update(
With( dtsum ),
Match Columns( Expr( Parse( "dt0:" || ColsUnique[1] || " = dtsum:" || ColsUnique[1] ) ) ),
Add Columns from Update Table( none ),
Replace Columns in Main Table( Expr( colName ) )
);
Close( dtsum, noSave );
Stop();
);
Expr(
Name Expr( mainCommand ) // will never execute - ut needed for the automatic update : )
)
;
)
)
);
dt << run formulas();
Workflows\BuildingBlocks\ColumnOperations\RankUnique_with_ByGroup.jmpflow