6
$\begingroup$

If I am not mislead by my memory, in Mathematica 6 a function Refresh was introduced, having the options UpdateInterval and TrackedSymbols, to be used inside Dynamic, that did not have these options. Anyway, nowadays both Dynamic and Refresh have these options, and already many years I never use Refresh, with the feeling that it is a relict from long ago.

In (What is the point of Refresh if Dynamic has an UpdateInterval option?), more or less the same was remarked by @Tom Wellington:

I haven' t found an example where I can' t get rid of Refresh[].

In the answer to that question, an example from the documentation was given. But in that example, we can replace Refresh with Dynamic with the same effect, so for me the question remains open.

Now I happened to find the following behaviour:

Column[{Dynamic[RandomReal[],UpdateInterval->0.5],
  Slider[Dynamic[RandomReal[],UpdateInterval->0.5]],
  Slider[Dynamic[Refresh[RandomReal[],UpdateInterval->0.5]]]}]

The first Dynamic updates normally. With the same expression in a slider, it does not update at all, but when we wrap it in Refresh, it updates.

Another example:

Column[{Slider[Dynamic[x]],
  Slider[Dynamic[1-x, TrackedSymbols:>{}]],
  Slider[Dynamic[1-x, TrackedSymbols:>{},UpdateInterval->0.5]],
  Slider[Dynamic[Refresh[1-x, TrackedSymbols:>{},UpdateInterval->0.5]]]}]

When we move the first slider, the second and the third slider are immediately updated, while the second slider should not update at all and the third slider only two times per second. The last slider, with the first argument wrapped in Refresh, behaves as expected.

It looks a little bit that we can use the options TrackedSymbols and UpdateInterval in Dynamic only when this Dynamic is a 'stand-alone' expression. As soon as it is part of a controller, we have to use Refresh if we want to use these options. I have not seen this documentated. Am I right?

$\endgroup$
4
  • $\begingroup$ There is a difference in the cell expressions that might be significant. My LinkSnooper is broken since our license renewal a couple of days ago, so I can only speculate. (The plain Dynamic is a DynamicBox; the slider is a SliderBox with code, no DynamicBox-es.) $\endgroup$
    – Michael E2
    Nov 6, 2014 at 13:16
  • $\begingroup$ @MichaelE2 Looks like Slider with Refresh inside behaves as standalone Dynamic[1 - x, TrackedSymbols :> {}, UpdateInterval -> .5]. I mean service request to update object, then call from FE on pre.link and return from kernel. It even stops when you minimize window. $\endgroup$
    – Kuba
    Nov 6, 2014 at 13:35
  • $\begingroup$ So it seems this should be the answer to linked topic :) $\endgroup$
    – Kuba
    Nov 6, 2014 at 13:37
  • $\begingroup$ @Michael. That is a very interesting remark, and it could well be the explanation. However, in all cases the contents of the Sliderbox is a Dynamic expression as in the kernel command, with the kernel variable x replaced with the frontend variable $CellContext`x. So it might be that the options for Dynamic do not work for frontend variables or maybe the frontend function Dynamic does not have these options. Anyway, I feel this could/should be documentated. $\endgroup$ Nov 6, 2014 at 13:52

1 Answer 1

3
$\begingroup$

It seems to me now that my conjecture was too simple. Here is an example that shows that we might need Refresh inside a Dynamic that is visualized on the screen in a DynamicBox. The crucial point is that this DynamicBox is displayed in a DynamicModuleBox, which has an effect on the updating procedure.

Consider the following example:

Panel[Column[{
  Dynamic[x],
  Button["press",x=0;Pause[2];x=RandomInteger[{1,9}], Method->"Queued"]
}]]

When the button is pressed, during two seconds the value 0 is displayed and then the new value of x turns up. The option Method->"Queued" is essential for this behaviour.

Now I want to localize the variable x in a DynamicModule.

DynamicModule[{x},
  Panel[Column[{
    Dynamic[x],
    Button["press",x=0;Pause[2];x=RandomInteger[{1,9}], Method->"Queued"]
 }]]
]

The output shows the name of the variable that the kernel will use for the variable x in the displayed DynamicModule. Before pressing the key, evaluate the next command, with 00 replaced with the value shown.

Dynamic[FE`x$$00]

Then press the key. Before the new value is displayed, the Dynamic output shows the value 0, but the DynamicModule does not.

The reason is that when the key is pressed, the DynamicModule sends the button action to the kernel for evaluation, waits for the completion of this action and then adapts the display. The evaluation setting Method->"Queued" is used only for the auxilary kernel variable FE`x$$00.

Is it possible to force the DynamicModule to update the inner Dynamic[x] during the kernel evaluation, as with Dynamic not wrapped in DynamicModule?

The answer is yes and the solution is given by Refresh:

DynamicModule[{x},
  Panel[Column[{
    Dynamic[Refresh[x, TrackedSymbols:>{x}]],
    Button["press",x=0;Pause[2];x=RandomInteger[{1,9}], Method->"Queued"]
  }]]
]

See also the latest answers given to Tom Wellingtons' question mentioned above.

$\endgroup$
5
  • $\begingroup$ One doesn't need Refresh there is some difference between Dynamic[x] and Dynamic[something more complex than symbol name]. Undocumented and causing a lot of headaches :/. The point is: Dynamic[List@x] will do too. $\endgroup$
    – Kuba
    Nov 17, 2015 at 19:59
  • 1
    $\begingroup$ Highly surprising. Dynamic[x] fails without Refresh, but Dynamic[x+0] and Dynamic[1*x] work without. Dynamic[Identity[x]] and Dynamic[#&[x]] fail. In these examples we have a non-atomic argument of Dynamic that evaluates to x. I cannot imagine that this is as intended. Could you post a question on this different behaviour of Dynamic? $\endgroup$ Nov 18, 2015 at 7:39
  • 1
    $\begingroup$ Yep, those hidden rules are terrible, not that I can't agree, but because I don't know what to expect :) I will try to post a question later but I suppose only John can answer ;) $\endgroup$
    – Kuba
    Nov 18, 2015 at 7:41
  • $\begingroup$ Sorry for a delay, done: 100828 $\endgroup$
    – Kuba
    Nov 30, 2015 at 13:19
  • $\begingroup$ other related questions/answers: 86401, and 103354 $\endgroup$ Jan 29, 2017 at 15:24

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.