2
$\begingroup$

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}]
```
$\endgroup$

2 Answers 2

4
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$ Commented Sep 6, 2021 at 13:31
  • $\begingroup$ @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. $\endgroup$ Commented Sep 6, 2021 at 13:44
2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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) $\endgroup$ Commented Sep 6, 2021 at 13:37
  • $\begingroup$ @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. $\endgroup$ Commented Sep 7, 2021 at 11:57

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.