# Can I plot *after* setting slider values?

I have some fundamental misunderstanding of the use of Mma's GUI elements, and I would appreciate some guidance. I would like to be able to set up a calculation based on slider values and then press a button to do the calculation. The calculation may be lengthy, so I do not want it dynamically updated. I think (?) I can illustrate the issue with a plotting example that does not work. Can I set the parameter in a slider but only update the following plot when I press a button?

ClearAll[h, k]
h[k_] := Plot[Sin[k x], {x, 0., 2. Pi}, PlotLabel -> Sin[k x]];
DynamicModule[{k = 1},
Column[{h[Dynamic@k],
Row[{Slider[Dynamic@k, {Range[10]}], Dynamic@k, Button["plot", h[k]]}, Spacer[2]]}]]


I hope this failed attempt will highlight my misunderstanding of the core issues, as I would like to receive some conceptual guidance. I usually find the Mma docs amazingly helpful, but not in this case.

• Who is the audience for this GUI? Is it enough that the button effects a Shift+Enter so it evaluates a cell for instance? Some more info on the use-case would be nice. May 27 '21 at 13:06
• @MariusLadegårdMeyer I don't think the context matters much, but consider a notebook of simulation models for students to experiment with interactively. Functions will be defined globally but the model parameters should be set (via a GUI) in an isolated (per model) way. (Thus the DynamicModule wrapping the controls.) The important thing is to be able to change the parameters before initiating the computation. See typical NetLogo models for illustrative examples.
– Alan
May 27 '21 at 13:41
• While I think its a good idea to learn more about Dynamic, here is solution using built-in options to Manipulate: Manipulate[h[k], {k, 1, 10}, ContinuousAction -> None]. The key piece is ContinuousAction.
– chuy
May 27 '21 at 19:36
• @chuy That's great, but (iiuc) it won't meet my needs, since there will be multiple sliders to set before the expression should be evaluated. Thanks!
– Alan
May 27 '21 at 19:57
• It should behave the same way with multiple controls. It won't update until the 'U' button is pressed.
– chuy
Jun 1 '21 at 15:58

Let the button update a variable: "kk", that will govern the plot:

ClearAll[h, k, kk]
h[k_] := Plot[Sin[k x], {x, 0., 2. Pi}, PlotLabel -> Sin[k x]];
DynamicModule[{k = 1},
Column[{Dynamic@
h[kk], {Row[{Slider[Dynamic@k, {Range[10]}], Dynamic@k,
Button["plot", kk = k]}, Spacer[2]]}}]]


Before clicking "Plot"

After clicking "Plot":

• Cool. If I move kk into the module variables this does what I want. I had focused on applying dynamic to variables, not to compound expressions. Can you elaborate on the meaning of "displayed form" in the docs for Dynamic?
– Alan
May 27 '21 at 15:30
• "displayed form" means what you see on screen, not the actual code that you can get with "FullForm" May 27 '21 at 17:13
• Yeah, I got that. But I don't know how to interpret it in the context of the docs. I think the relevant text to improve my understanding is rather this: "Dynamic[expr] evaluates expr whenever it needs its value and it determines that any type of value of symbols on which it depends might have changed."
– Alan
May 27 '21 at 20:01
• In that documentation, however, the phrase "whenever it needs its value" is mysterious to me.
– Alan
May 27 '21 at 21:22
• My interpretation: when it needs the value and in addition, it notes that the value has changed. May 28 '21 at 7:55