1
$\begingroup$

What is the correct way to stop all my Manipulate cells from breaking every time I issue ClearAll["Global`*"] command?

Here's an attempt below, it does not survive ClearAll["Global`*"], as the definition of clean does not get saved.

x = Range[100];
clean[x_] := Module[{y = 5}, x*5];

With[{xLocal = x, cleanLocal = clean},
 Manipulate[
  ListPlot[cleanLocal[xLocal], PlotLabel -> k], {k, 1, 5, 1}, 
  SaveDefinitions -> True]
 ]

Here's an example of what I see (things break if ClearAll["Global`*"] is run in a different cell than Manipulate)

enter image description here

$\endgroup$
10
  • $\begingroup$ You could "localize" your definitions to the With or some other localizing construct. You could put your definitions into a context other than Global (although I suspect that's not really the kind of thing you were looking for). $\endgroup$
    – lericr
    Nov 22, 2023 at 22:43
  • $\begingroup$ What I mean by the "localizing" comment is to effectively inline things or make them anonymous: With[{xLocal = Range[100], cleanLocal = Module[{y = 5}, #*5] &}, ... ] $\endgroup$
    – lericr
    Nov 22, 2023 at 22:46
  • $\begingroup$ I used With to localize variables above, however that fails $\endgroup$ Nov 22, 2023 at 22:52
  • 1
    $\begingroup$ Maybe I'll try to be even clearer... In the WolframLanguage, there are no truly local variables. Everything is global. That doesn't mean everything is in the Global context. I just mean that every symbol is accessible from anywhere--you just need to know its name (the full name including context path). The way some "localizing" constructs (like Module) work, is to just create special, difficult-to-accidentally-refer-to names. $\endgroup$
    – lericr
    Nov 23, 2023 at 0:00
  • 1
    $\begingroup$ I don't think SaveDefinitions works the way you think it does. The saved definitions are executed when the DynamicModule is instantiated in the front end. If you change the defnition of clean while the Manipulate is running, Maniupulate will not re-execute the save definition. Since clean is global, its value is shared. Clear it, and it is undefined throughout the kernel. Copy a broken Manipulate output and paste it, and the saved definition will be executed again, restoring the original definition. Localize the symbol clean and its definition in Manipulate to protect it. $\endgroup$
    – Goofy
    Nov 23, 2023 at 3:37

1 Answer 1

1
$\begingroup$

You can use Copying one symbol into another and store ClearAll-persistent values as DynamicModule variables.

copySymbol // Attributes = {HoldAll}
copySymbol[new_Symbol, old_Symbol] := Language`ExtendedDefinition[new] = 
  Language`ExtendedDefinition[old] /. HoldPattern[old] :> new
x = Range[100];
clean[x_] := Module[{y = 5}, x*5];


DynamicModule[{xLocal = x, cleanLocal}
, copySymbol[cleanLocal, clean]

; Manipulate[
    ListPlot[cleanLocal[xLocal], PlotLabel -> k]
  , {k, 1, 5, 1}
  ]
]

SaveDefinitions is not needed because it saves other than DM's variables and loads them on initialization only. They are still Global` so it will break.

For DynamicModule variables, their values are saved automatically (watch out for SetDelayed OwnValues maybe) and shielded from ClearAll because they end up in FE` context.

But maybe it is worth adjusting your workflow so that Clear["Global`*"] won't affect your Manipulates. A specific solution would require more insight into your work though.

$\endgroup$
4
  • $\begingroup$ Thanks.....is this method robust against restarting Mathematica? Ie, I want my demos to still work when I reopen the notebook? Or is the "copy all relevant code inside manipulate block" the only reasonable way? $\endgroup$ Nov 23, 2023 at 15:34
  • $\begingroup$ @YaroslavBulatov have you tried, I think it should work. $\endgroup$
    – Kuba
    Nov 23, 2023 at 15:55
  • $\begingroup$ Yes it works for this example, thanks! BTW why is it that you need to call copySymbol for clean but not for xLocal, is it because of DownValues that don't get copied by DynamicModule? $\endgroup$ Nov 23, 2023 at 17:03
  • $\begingroup$ @YaroslavBulatov x evaluates to a range so the symbol reference is gone. clean does not evaluate to anything so cleanLocal still references that symbol which may be cleared. $\endgroup$
    – Kuba
    Nov 23, 2023 at 22:02

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.