5
$\begingroup$

I am attempting to creating a UI dialogue that updates itself as selections are made. The issue is the UI is not updating itself even though the dependent variables are being updated. A very minimal example follows.

v =.;
Panel[
 Column@{
   Row[{"Pick", 
     CheckboxBar[Dynamic@v, # -> IntegerName@# & /@ Range[3]]}],
   Dynamic[
    Refresh[
     If[ValueQ[v],
      v + 1,
      "Pick something"
      ],
     TrackedSymbols :> {v}
     ]
    ]}
 ]

What I am expecting to happen is that once v is assigned a value that the If will update to show the calculation. However, v is updated but the Dynamic does not update.

In the actual case the user select a value from a PopupMenu. This value is used to query a database with the resultset being used in the next PopupMenu. Similar to the above except there are some function calls. The variable v must initially be clear.

Ideas?


Update

I had some to and fro emails with WRI and I think they are looking into why ValueQ does not re-evaluate in the If. I have worked out that I can use v = Null; to start and v =!= Null in the If to get things to work as expected.

$\endgroup$
  • $\begingroup$ The expression is refreshing fine, as you can see by using If[v, v + 1, "Pick something"] instead of your If statement. It's the ValueQ that is not behaving as you expected. I wonder if the fact that CheckboxBar always returns a list is getting in the way. $\endgroup$ – MarcoB Jan 31 '17 at 21:20
  • 1
    $\begingroup$ @Kuba I have reported to WRI. Ugh, it is going to be one of those weeks, me'thinks. $\endgroup$ – Edmund Jan 31 '17 at 21:35
  • $\begingroup$ @Kuba In the actual I am testing an integer variable so I think I have to go with ValueQ unless there is another way to test if an integer variable as a value. $\endgroup$ – Edmund Feb 1 '17 at 9:48
  • $\begingroup$ @Edmund IntegerQ? $\endgroup$ – Kuba Feb 1 '17 at 9:52
2
$\begingroup$

I also think this is a bug along the lines of this one. There are obviously certain cases where the tracking dependencies are not resolved correctly. Usually there are simple things you can change in your code to get the code working as intended anyway, but unfortunately no simple pattern which always works well. Here I would suggest something like this:

v =.;
Panel[
  Column[{
    Row[{
      "Pick", 
      CheckboxBar[Dynamic@v, # -> IntegerName@# & /@ Range[3]]
    }],
    Dynamic[
      Refresh[
        v;
        If[ValueQ[v],
          v + 1,
          "Pick something"
        ],
        TrackedSymbols :> {v}
      ]
    ]
  }]
]

Of course that is just a workaround, but that is probably all you can expect in this case...

$\endgroup$
  • $\begingroup$ fyi, my guess is that the problems like this appear when MMA tries to optimize things and assumes that the FrontEnd can handle things, which it can't. So here If is present also in FEPrivate` context but somone forgot to check the content. $\endgroup$ – Kuba Feb 16 '17 at 13:53
1
$\begingroup$

This may be a comment with code. I am confused as to the exact part of the code that is not refreshing.

It seems to me that the complication may arise from the use of ValueQ on the output of CheckboxBar, which is not a symbol, but a list.

Consider the following variation on Edmund's code, in which

  • instead of CheckboxBar (which returns a list) I am using SetterBar (which returns a single value);
  • instead of a string value, I assign to v a True / False value;
  • instead of checking whether v has a value with ValueQ, I simply use the dynamically updated value.

    v =.;
    Panel[
     Column@{
       Row[{"Pick", SetterBar[Dynamic@v, {False, True}]}], 
       Dynamic[Refresh[If[v, "True was selected", "Pick something"], TrackedSymbols :> {v}]]
       }
     ]
    

Initially, this evaluates to:

false

After selecting True, this changes to:

True

In fact, this works even without the Refresh:

v =.;
Panel[
 Column@{
   Row[{"Pick", SetterBar[Dynamic@v, {False, True}]}], 
   Dynamic[If[v, "True was selected", "Pick something"]]
  }
]

False - no refresh True - no refresh

$\endgroup$

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.