Collection of useful resources on the web:
- There is a wonderful lecture by @joseph_morgan in the Discovery Summit Series:
https://community.jmp.com/t5/Abstracts/Using-JSL-to-Develop-Efficient-Robust-Applications-EU-2018-415/ev-p/849638
Joseph explains the secrets and possibilities of Expression Handling in JSL - and the trapdoors associated with the functionality.
The lecture is based on the content of Expression Handling Functions: Part I - Unraveling the Expr(), NameExpr(), Eval(), ... Conundrum
- Writing JSL code dynamically by @Jasean
(step-by-step guide through Universal Tricks #1 & #2, see below)
- The Difference between Strings and Expressions - in 50 lines:
reply to a community post by @Craige_Hales
- Nice Course with many application cases: Session 9: Advanced JSL , @jthi
- further info can be found in the Scripting Guide: advanced-expressions-macros-and-lists
Basics
internal:
#062 - expression handling in JSL
#092 - expression handling
| 1 |
don't evaluate the expression (2+1), just store it in the variable x |
x= Expr(2+1) |
| 2 |
get the expression which is stored in x |
Name Expr(x) |
| 3 |
force evaluation |
Eval(myexpr) |
| 4 |
evaluate xyz and replace Expr(xyz) with the return value |
Eval Expr( ... Expr(xyz)) |
*) NB: in 1 and 4, it's the same function "Expr(...)" - but with the opposite effect:
In 1, the content of Expr doesn't get evaluated, in 4 JUST the content of Expr gets evaluated
3 Universal Tricks
| 1 |
pre-evaluate parts of an expression via Eval Expr() ... then evaluate the expression via Eval() solves 90%, (with option 2: 99%) of all issues |
Eval(Eval Expr ( ... Exp(eval 1st) ...)) option 2: Eval(Eval Expr ( ... Exp(Name Expr( )) ...))
|
| 2 |
Solves the same 99% like # 1 - option 2 |
Eval(substitute(Expr(), Expr(), Name Expr() ))
|
| 3 |
ultimate trick for column references ( @jthi, e.g. Session 9: Advanced JSL @33min ) |
Name Expr(as column("column name" or :column)) |
NB:
Some functions evaluate their arguments, some don't, some evaluate some of their arguments - there are functions like head() which evaluate their arguments to a certain degree: Does Head evaluate its argument? - and others evaluate their argument but if it's a single variable, they behave like Name Expr(): Exceptions are the utilities Show, Write, and Print ...
Unfortunately, most of these details are not available for the users. The users have to guess.
Here is a wish for JSL editor to help the users by showing this information interactively: Advanced syntax highlighting in JSL Editor - does the function evaluate it's argument?
-> If you want to get this automatic support, please follow the link and vote!
Examples from the community:
| Topic |
Links |
Details |
|
Column() vs.
As column() vs.
name()
|
Why do these ways of referencing columns give different results?How to replace :column_name with variable in JMP JSL?Column() vs As Column() vs datable:column vs dt:As name("column")
|
the universal syntax to reference a column is :column. But often, it's tricky to get the ":column" right ...
|
| Column references in column formulas |
Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute
Trying to create a column and fill it with a formula but I get an error.
Use expr() to replace variable column names in a formulaAdding Formula Columns based on Column Name ListCreating a formula column from a list ( JSL )Need help with expressions, column references, formulas and column renamingCreate new columns by loop formula calculation with specific column namesJSL to create formula column with variable column names Column name from string
Script concatenate columns
Using Expr() and Eval() with variables name inside a loopUse column variable in IF expression
JSL: Creating formula using a variable column name
Using variables to create formula columnscreating formula using existing column names
Iterating Columns
|
define the column via
col = Name Expr(As Column(colname))
and insert it with Eval(Substitute())
Alternatives:
As Column (Expr(colname))
with Eval(Eval Expr())
or
Expr(Name Expr(AsColumn(dt, colname)))
with Eval(Eval Expr()) ...
|
| Column references in platform calls and messages |
JSL help proper referencing of a columnRun a Platform without Knowing the Number or Names of Columns
Distribution using column list
JSL: help properly referencing a data table column for Distribution platform
|
use
Name Expr(As Column(colname))
with Eval(substitute())
... or if you have issues with a column list, just add eval() or evallist()in the column() argument
|
| use a column reference in a function call |
Column reference in custom functionCustom Function - how to reference the column
|
- wrap the column with Name Expr()
- use a list
|
|
construct an expression
|
Can you construct this without writing expression as a string?Run a Platform without Knowing the Number or Names of ColumnsUsing list of columns in formulas
How to script an aggregate column from a variable list of column references
|
- start with an empty draft and fill it via insert into(draft, arguments)
- use substitute
- create a list, and substitute list with another JSL symbol
|
|
further workarounds via Expression handling
|
Exclude rows in Col Quantile Calculation
|
prevent inappropriate caching in Col Quantile by using an expression.
|
| From version to version, less expression handling is needed to make things work |
link? |
|
[More Topic Containers like this]
Hi @hogi ,
Thanks for consolidating this information and bringing a lot of this information together. The talk sounds really informative.
I do like how you can do things several different ways in JMP and in JSL, but sometimes there are too many different ways that are very specific to the case at hand, and this can become somewhat cumbersome when trying to make code that is generic and able to handle many different scenarios.
Thanks!,
DS
If you don't use the tricks:
JSL and variables - 3 issue you should be aware off when writing JSL code.
Different issues, always the same solution : )
the solution?
-> just read the main post.
c= 5;
col = Name Expr(:height);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
gb = graph Builder(); framebox = report(gb)[FrameBox(1)];
// issues with variables in a message
// all 3 of them look quite innocent
// but none of them will survive a restart
New Column ("rescaled", Formula (c * :height));
dt << New Column ("rescaled", Formula (5 * col));
framebox << Add Graphics Script(Y Function( c * x, x ) );
// no issue here:
dt << concatenate(dt);
dt << new column(c, formula(3));One could think: don't ever use a variable in a message!
But this goes way too far - the last 2 examples don't have an issue.