3
$\begingroup$

Here is my code that is not working.

vars = <|"x" -> <|"Q" -> "Quant", "bins" -> {10, 20, 30, 40}|>,
"y" -> <|"Q" -> "Qual", "bins" -> {"Male", "Female"}|>|>;

doVarSpecs[vars_] := DialogInput[
DynamicModule[{varlist, var, oplistquant, oplist, op, vallist, val},
g[var_] := Block[{},
 oplist = 
  If[vars[var, "Q"] === "Quant", Keys@oplistquant, 
   Take[Keys@oplistquant, 2]];
 op = First@oplist;
 Print[op, oplist];
 vallist = vars[var, "bins"]; val = First@vallist;
 ];
varlist = Keys@vars; var = First@varlist;
 oplistquant = {"=" -> SameQ, "\[NotEqual]" -> UnsameQ, 
 "\[LessEqual]" -> LessEqual, "\[GreaterEqual]" -> GreaterEqual};
 g[var]; Print[varlist]; Print[oplist]; Print[vallist];
 Column[{
  Style["Choose a Set of Variable Relations", 22, Darker@Blue],
  "  ",
  Row[{
   "Variable: ",
   PopupMenu[Dynamic[var, (var = #; g[var]) &], varlist], "  ",
   Dynamic[PopupMenu[Dynamic[op], oplist], 
    TrackedSymbols :> {oplist}], "  ",
   Dynamic[PopupMenu[Dynamic[val], vallist], 
    TrackedSymbols :> {oplist}]
   }],
  "  ",
  Row[{Button["OK", DialogReturn[{var, op, val}]], CancelButton[]}]
 }
 ]
], WindowTitle -> "Variable Specification Selector"]

For a simpler case, the question was answered previously,

My previous similar question

In this case, when var is changed, g[var] is called and both oplist and vallist are updated, but the PopupMenu controls that use them are not updated.

The assistance of the Stack Exchange community would be greatly appreciated.

$\endgroup$

1 Answer 1

1
$\begingroup$

I found a mechanism that works for my needs in this case. The key seems to be embedding pure functions (making all of the symbols visible to Dynamic). A code fragment follows:

doVarSpecs[vars_] := DialogInput[
 DynamicModule[{varlist, var, oplist, oplist1, oplist2, op, val},
 varlist = Keys@vars; var = First@varlist;
 oplist = <|"=" -> SameQ, "\[NotEqual]" -> UnsameQ, 
  "\[LessEqual]" -> LessEqual, "\[GreaterEqual]" -> GreaterEqual|>;
 oplist1 = Keys@oplist; oplist2 = Take[oplist1, 2];
 q = vars[var, "Q"] === "Quant";
 Column[{
  Style["Choose a Set of Variable Relations", 22, Darker@Blue],
  "  ",
  Dynamic@Row[{
    "Variable: ",
    PopupMenu[
     Dynamic[var, (var = #; q = (vars[var, "Q"] === "Quant"); 
        op = First@oplist1; val = First@vars[var, "bins"]) &], 
     varlist, MenuStyle -> 18],
     "  ",
    PaneSelector[{
      True -> PopupMenu[Dynamic[op], oplist1, MenuStyle -> 20],
      False -> PopupMenu[Dynamic[op], oplist2, MenuStyle -> 20]
      }, q], "  ",
    Dynamic@
     PopupMenu[Dynamic[val], vars[var, "bins"], MenuStyle -> 18]
    }],
   "  ",
   Row[{Button[" OK ", DialogReturn[expr], BaseStyle -> 18], "  ",
    Button[" Cancel ", DialogReturn[{}], BaseStyle -> 18]}]
   }, BaseStyle -> 18]
  ], WindowTitle -> "Variable Specification Selector" ]

Example call

vars = <|"Age" -> <|"Q" -> "Quant", 
    "bins" -> {10, 20, 30, 40, 50, 60, 70, 80}|>,
 "Gender" -> <|"Q" -> "Qual", "bins" -> {"Male", "Female"}|>,
 "Race" -> <|"Q" -> "Qual", 
    "bins" -> {"White", "Black", "Hispanic", "Asian", "Other"}|> |>

doVarSpecs[vars]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.