1
$\begingroup$

I am having a problem with applying Values to a list of rules within a dynamic module.

The list of rules is in the following format {a -> 1, b -> 2, ...} where all the keys are the variable names of an association (e.g., a = <|x1 -> 3,x2 -> 2|>) and the values are just integers. I used

associationNames = HoldForm[#] & /@ Unevaluated /@ Unevaluated@{a, b, c}

to get a list of the associations names (if I hadn't, the list of rules would evaluate and become

{<|x1 -> 3, x2 -> 2|> -> 1, ... }.

Inside my dynamic module the list of rules is generated by

MapThread[Rule,{associationNames, amounts}

where amounts come from input fields. When I try to call Values on the list of rules, it does not evaluate. It just outputs

Values[{a -> 1, b -> 2}].

EDIT (Code Added)

a = <|x -> 1, y -> 2|>;
b = <|x -> 3, y -> 4|>;
variableList = HoldForm[#] & /@ Unevaluated /@ Unevaluated@{a, b};
DynamicModule[
 {dropDown = variableList, dropDownValue,
  variableName = {},
  amounts = {},
  listOfRules
  },     
 Column[{
   Row[{
     PopupMenu[Dynamic[dropDownValue], dropDown],
     Button["+",
      {
       variableName = variableName~Append~dropDownValue,
       amounts = amounts~Append~0
       }
      , Alignment -> Center, Appearance -> "Palette"]
     }],
   Dynamic[
    Column[{
        Panel[
         Row[{#,
           Button["x",
            {amounts = 
              amounts~Delete~Flatten[Position[variableName, #]],                 
             variableName = 
              variableName~Delete~Flatten[Position[variableName, #]]}
            ],               
           InputField[
            Dynamic[amounts[[Flatten@Position[variableName, #]]]]]
           }]
         ]
        }] & /@ variableName
    ],
   {"Amount: ", Dynamic[amounts]},
   {"Name: ", Dynamic[# & /@ variableName]},
   listOfRules = Dynamic@MapThread[Rule, {variableName, amounts}];
   Values[listOfRules]
   }]
 ]

After using the top controls to input values for a and b

{Amount: ,{0,0}}
{Name: ,{a,b}}
Values[{a -> 0, b -> 0}]
$\endgroup$
  • $\begingroup$ Please provide all relevant code in a form that readers can copy and execute. $\endgroup$ – bbgodfrey Jun 28 '17 at 0:43
  • $\begingroup$ I'm working on simplifying the code to post (as it is very long). I just posted the question first to see if this was a simple problem occurring with HoldForm[] and Unevaluted[]. $\endgroup$ – I should change my Username Jun 28 '17 at 0:49
2
$\begingroup$

I'm not sure this is quite what you want, but it fixes your immediate problem. It might give you a start on moving forward.

Basically your problem is two-fold: not applying Dynamic in the right places (often a tricky business), and not putting everything need into 1st Column expression.

a = <|x -> 1, y -> 2|>;
b = <|x -> 3, y -> 4|>;
variableList = HoldForm[#] & /@ Unevaluated /@ Unevaluated @ {a, b};
DynamicModule[
  {dropDown = variableList, dropDownValue, variableName = {}, 
   amounts = {}, listOfRules}, 
  Dynamic @ 
   Column[
    {Row[
       {PopupMenu[Dynamic[dropDownValue], dropDown], 
        Button["+", 
          {variableName = variableName ~ Append ~ dropDownValue, 
           amounts = amounts ~ Append ~ 0}, 
          Alignment -> Center, 
          Appearance -> "Palette"]}], 
     Column[
       (Panel[
          Row[
            {#, 
             Button["x", 
               {amounts = amounts ~ Delete ~ Flatten[Position[variableName, #]], 
                variableName = variableName ~ Delete ~ 
                  Flatten[Position[variableName, #]]}], 
             InputField[
               Dynamic[amounts[[Flatten @ Position[variableName, #]]]]]}]] & 
         /@ variableName)],
     {"Amount: ", amounts},
     {"Name: ", amounts# & /@ variableName}, 
     listOfRules = MapThread[Rule, {variableName, amounts}]; Values[listOfRules]}]]

dynamic

$\endgroup$
  • $\begingroup$ That worked out splendidly. Why does applying Dynamic[] to Values[] (i.e. Dynamic[Values[x]]) not evaluate, but applying Dynamic[] to the whole Column does (since listofRules still dynamically updates)? $\endgroup$ – I should change my Username Jun 28 '17 at 7:51
  • $\begingroup$ @IshouldchangemyUsername. Think of Dynamic as a tag that means "look inside this form for variables have changed and update it when true". The front-end didn't see any variables it was tracking inside Values[listOfRules]; i.e., listOfRules isn't being tracked. Note: I edited the answer to remove two other Dynamic wrappers -- those being wrapped around the output of "Amount" and "Name". No longer needed because the Dynamic I placed around Column takes care of them too. $\endgroup$ – m_goldberg Jun 28 '17 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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