And here are few of the very common problems you might come across and how you can solve them with expressions and by evaluating them when needed.
1. Using constant in a formula
Problem:
Your formula works only once and looks like this after save and re-open:
Solution:
Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute . Most of the time I use Eval(EvalExpr()).
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
value_to_add = 5;
Eval(EvalExpr(
dt << New Column("HeightAddFive", Numeric, Continuous, Formula(
Expr(value_to_add) + :height
));
));
2. Using a constants in graphic script
Problem:
You end up with a script like this
Graph Builder(
Show Control Panel(0),
Variables(X(:height), Y(:weight), Overlay(:sex)),
Elements(Points(X, Y, Legend(1)), Smoother(X, Y, Legend(2))),
SendToReport(
Dispatch(
{},
"Graph Builder",
FrameBox,
{Add Graphics Script(
7,
Description(""),
Pen Color(color_to_use);
Pen Size(size_to_use);
H Line(value_to_add);
)}
)
)
);
Solution:
Use Eval(EvalExpr()) (or Substitute() is other good option)
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
value_to_add = 80;
gb = dt << Run Script("Graph Builder Smoother Line");
color_to_use = "Red";
size_to_use = 2;
Eval(EvalExpr(
Report(gb)[FrameBox(1)] << Add Graphics Script(
Pen Color(Expr(color_to_use));
Pen Size(Expr(size_to_use));
H Line(Expr(value_to_add));
);
));
3. Setting variables as spec limits
Problem:
Spec limits are not being set correctly
Solution:
Start by letting JMP create code (add column property and take the code from Enhanced Log or right click on the column and "Copy Column Properties". You get something like this
Data Table("Big Class"):height << Set Property(
"Spec Limits",
{LSL(11), USL(22), Target(12), Show Limits(1)}
);
Improve this script to use variables
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
aa_specs = Associative Array();
aa_specs["height"] = ["LSL" => 50, "USL" => 80];
aa_specs["weight"] = ["LSL" => 60, "USL" => 160];
For Each({col_name}, dt << Get Column Names("String"),
If(Contains(aa_specs, col_name),
Eval(EvalExpr(
Column(dt, col_name) << Set Property("Spec Limits" ,{
LSL(Expr(aa_specs[col_name]["LSL"])),
USL(Expr(aa_specs[col_name]["USL"])),
Target(Expr((aa_specs[col_name]["LSL"] + aa_specs[col_name]["USL"])/2)),
Show Limits(1)
})
));
);
);
4. Adding table scripts to table
Problem:
You end up with table script which says something like this
Solution:
Eval(EvalExpr()) with Name Expr(). This won't create the graph at all. If you want to run it use << Run Script("My nice graph") or evalute the expression
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
gb_expr = Expr(dt << Graph Builder(
Show Control Panel(0),
Variables(X(:height), Y(:weight), Overlay(:sex)),
Elements(Points(X, Y, Legend(1)), Smoother(X, Y, Legend(2))),
));
Eval(EvalExpr(
dt << New Script("My nice graph", Expr(Name Expr(gb_expr)));
));
// dt << Run Script("My nice graph");
// Eval(gb_expr);
but in cases like this I would most of the time use << Save Script to Data Table, but this will run the platform so remember to close it
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
gb_expr = Expr(dt << Graph Builder(
Show Control Panel(0),
Variables(X(:height), Y(:weight), Overlay(:sex)),
Elements(Points(X, Y, Legend(1)), Smoother(X, Y, Legend(2))),
));
gb = Eval(gb_expr);
gb << Save Script to Data Table("My nice graph");
gb << Close Window;
Wait(0);
5. Build formulas based on user selections from Col List Boxes
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
nw = New Window("Selections",
clb_x = Col List Box(dt, << Set items({"height"})),
clb_y = Col List Box(dt, << Set items({"weight"}))
);
x_selection = clb_x << get items;
y_selection = clb_y << get items; // these are strings
wait(2);
nw << Close Window;
// Using Eval(EvalExpr())
Eval(EvalExpr(
dt << New Column("Multiplication_EvalExpr", Numeric, Continuous, Formula(
Expr(NameExpr(AsColumn(dt, x_selection))) * Expr(NameExpr(AsColumn(dt, y_selection)))
));
));
// Using Eval(Substitute())
Eval(Substitute(
Expr(dt << New Column("Multiplication_EvalSubstitute", Numeric, Continuous, Formula(_xcol_ * _ycol_))),
Expr(_xcol_), Name Expr(AsColumn(dt, x_selection)),
Expr(_ycol_), Name Expr(AsColumn(dt, y_selection))
));
6. Use expressions to build user-interfaces (not the best example)
Problem:
Code gets clunky
Names Default To Here(1);
nw = New Window("asdas",
Button Box("Perform Action1",
Caption("Perform Action1")
),
Button Box("Perform Action2",
Caption("Perform Action2")
),
Button Box("Perform Action3",
Caption("Perform Action3")
)
);
Problem2:
My code doesn't work (might happen as window doesn't always know about all variables)
Names Default To Here(1);
caption_expr = Expr(
Caption("Perform Action");
);
nw = New Window("asdas",
Button Box("Perform Action1",
caption_expr
),
Button Box("Perform Action2",
caption_expr
),
Button Box("Perform Action3",
caption_expr
)
);
Solution1:
Evaluate the expression
Names Default To Here(1);
caption_expr = Expr(
Caption("Perform Action");
);
Eval(EvalExpr(
nw = New Window("asdas",
Button Box("Perform Action1",
Expr(Name Expr(caption_expr))
),
Button Box("Perform Action2",
Expr(Name Expr(caption_expr))
),
Button Box("Perform Action3",
Expr(Name Expr(caption_expr))
)
);
))
Solution2:
In this case you could also use << Set Function(Function([}))
Names Default To Here(1);
nw = New Window("asdas",
Button Box("Perform Action1", << Set Function(function({this},
Caption(this << get button name);
))),
Button Box("Perform Action2", << Set Function(function({this},
Caption(this << get button name);
))),
Button Box("Perform Action3", << Set Function(function({this},
Caption(this << get button name);
)))
);
Solution3:
Combine
Names Default To Here(1);
button_caption_expr = Expr(
Caption(this << get button name);
);
Eval(EvalExpr(
nw = New Window("asdas",
Button Box("Perform Action1", << Set Function(function({this},
Expr(NameExpr(button_caption_expr));
))),
Button Box("Perform Action2", << Set Function(function({this},
Expr(NameExpr(button_caption_expr));
))),
Button Box("Perform Action3", << Set Function(function({this},
Expr(NameExpr(button_caption_expr));
)))
);
));
Solution 4:
Use function (solution3 might be more robust)
Names Default To Here(1);
show_btn_caption = function({btn_ref}, {Default Local},
Caption(btn_ref << get button name);
);
nw = New Window("asdas",
Button Box("Perform Action1", << Set Function(function({this},
show_btn_caption(this);
))),
Button Box("Perform Action2", << Set Function(function({this},
show_btn_caption(this);
))),
Button Box("Perform Action3", << Set Function(function({this},
show_btn_caption(this);
)))
);
Solution5:
Use function to build the buttons
Names Default To Here(1);
create_btn = function({btn_title}, {Default Local},
btn = Button Box(btn_title, << Set Function(function({this},
Caption(btn_ref << get button name);
)));
return(btn);
);
nw = New Window("asdas",
create_btn("Perform Action1"),
create_btn("Perform Action2"),
create_btn("Perform Action3")
);
-Jarmo