# DynamicModule & Manipulate Plot

I tried to write a convenience wrapper around Manipulate for easier plotting. However I failed already at the very first step.

Consider the following example:

manipulatePlot[fun_, ran_, manran_] :=
DynamicModule[{f = fun, r = ran, manr = manran},
Manipulate[Plot[Evaluate[f], Evaluate[r]], Evaluate[manr]]
]

g[x_, a_] := a x^2

manipulatePlot[g[x, a], {x, 0, 2}, {a, 0, 2}]


The result is an empty plot... How do I get the plot displayed properly? The problem must be with the Plot function, because the following does work (i.e. a can be correctly manipulated):

manipulate[fun_, manran_] :=
DynamicModule[{f = fun, manr = manran},
Manipulate[Evaluate[f], Evaluate[manr]]
]

manipulate[g[x, a], {a, 0, 2}]
$$$$


There's no reason to use DynamicModule here, really. The following will work:

ClearAll[manipulatePlot]
SetAttributes[manipulatePlot, HoldAll];
manipulatePlot[expr_, plotSpec_, manipSpec_] := Manipulate[
Plot[expr, plotSpec],
manipSpec
];

g[x_, a_] := a x^2

manipulatePlot[g[x, a], {x, 0, 2}, {a, 0, 2}]


You need to hold the arguments to prevent premature evaluation of the variables if they have definitions already.

• That does indeed work, thank you! I've got two clarification questions: 1. Is there a deeper reason why DynamicModule is not recommended here, or is it because of the simplicity of the example that a regular function is enough? 2. Following your example, I realized that manipulatePlot[fun_, ran_, manran_] := DynamicModule[{}, Manipulate[Plot[fun, ran], manran] ] works. I wouldn't have suspected that this behaves differently than my initial example, but it does (Why?). Is this acceptable code or am I messing around with the localizing of variables? Sep 6, 2021 at 13:31
• @jabberwocky The short answers is essesntially: yes, you're messing around with the localisation of the variables. Whenever you're using Evaluate, you're essentially going around the default behaviours of functions. That's not necessarily a bad thing, but it can get confusing if you don't have a very clear mental picture for how all these functions work. BTW: the reason that DynamicModule[{}, ...] works, is because it essentially doesn't do anything. Sep 6, 2021 at 13:44

Another approach using DynamicModule:

mPlot =.
mPlot[func_, r1_, r2_] := DynamicModule[{a },
Column[{
Slider[Dynamic[a], r2],
Dynamic@a,
Dynamic[Plot[func[a, x], {x, First@r1, Last@r1}]]}]]


Since Manipulate generates a DynamicModule, there is no reason to wrap the two as you have originally tried.

• This is effectively rebuilding a Manipulate function from scratch, right? My vision was to avoid this and rely on the already existing Manipulate and make some tweaks to it (except if there is a good reason to avoid DynamicModule and Manipulate`) Sep 6, 2021 at 13:37
• @jabberwocky correct, and is an option only if you are unsatisfied with the way Manipulate is creating the output. More or less depends on what your current and future definition of "tweaks" is. Sep 7, 2021 at 11:57