4
$\begingroup$

I have a complex Manipulate with more than 10 controls. Each one has its own default value.

I'd like to add a custom button in the control area to reset all control's values to their default values.

That's exactly what the "Initial Settings" in the Manipulate menu (+ on the up left corner) does. But I'd like to bring this action to a custom button located near my custom play/pause buttons.

I've searched the forum and I've read that "Initial settings" is a sort of bookmark, created automatically (?).

Yet I can't find how to execute this option from a custom button (or call that sort of automatic bookmark from a different control).

I've found many posts about setting the bookmarks programmatically, but no answer on the simpler question about how to invoke the "Initial settings" option from outside the manipulate menu.

I know that I could assign all the default values one by one (within the custom button) but that seems silly to me since there's already a similar option in the + menu.

Any suggestion?

$\endgroup$
  • $\begingroup$ Are those answers useful? 31344 if so, I'd say it is a duplicate. $\endgroup$ – Kuba Jan 29 '15 at 17:19
  • $\begingroup$ No, I had read them. They just suggest to use the default AppearanceElements -> "ResetButton" (not a custom button) or use a custom button to set all controls to their default values, thing that I'd like to avoid (if possible) having a long list (possibly growing) of controls. $\endgroup$ – Luca M Jan 29 '15 at 17:26
  • 1
    $\begingroup$ I am not following your question. You mean you want to make a button on the fly somehow? Why not just code a button that when clicked resets the other control to their initial values? Like this: Manipulate[ Row[{a, b, c}], {{a, 6, "a"}, 0, 10, 1}, {{b, 7, "b"}, 0, 10, 1}, {{c, 8, "c"}, 0, 10, 1}, Button["Reset", a = 6; b = 7; c = 8] ] I must be not understanding the question. $\endgroup$ – Nasser Jan 29 '15 at 19:24
  • $\begingroup$ @Nasser Because this is not fun at all! :) $\endgroup$ – Kuba Jan 29 '15 at 19:35
  • $\begingroup$ @Nasser I was just wandering if the same operation accomplished by your code could be made more easily and in a smarter way through some calling to the built-in "Initial Settings" option. Imagine having 20 controls and tweaking tentatively their default values to find the best working Initial Settings for a demonstration. Following your suggestion I'd have to change their default values twice (in each control and in the reset button). Or, alternatively, I'd have to add 20 new variables to the code to set the default values and to be then used both in the controls and in the Reset button. $\endgroup$ – Luca M Jan 29 '15 at 19:39
4
$\begingroup$

Not completely tested, based on undocumented structure of the DynamicModule constructed by Manipulate -- but it works for now:

Manipulate[a + b + c,
 {{a, 1}, 0, 5}, {{b, 2}, 0, 5}, {c, 0, 5},
 Button["Reset",
  Replace[
   Typeset`specs,
   {{{Hold[var_Symbol], val_}, ___} :> (var = val),
     {Hold[var_Symbol], val_, ___} :> (var = val)},
   1]]
 ]

Update. A combination of Kuba's and my ideas: We construct the code for the button from the code for the Manipulate. More likely not to break in future versions.

SetAttributes[addreset, HoldAll];
addreset[Manipulate[body_, specs___]] :=
 With[{btn = Button["Reset", CompoundExpression @@ #] &@Replace[
      Hold[specs],
      {{{var_Symbol, val_}, ___} :> (var = val),
       {var_Symbol, val_, ___} :> (var = val),
       _ -> Sequence[]},
      1]},
  Manipulate[body, specs, btn]
  ]

addreset@Manipulate[a + b + c,
  {{a, 1}, 0, 5}, {{b, 2}, 0, 5}, {c, 0, 5}
  ]
$\endgroup$
  • $\begingroup$ Nice, I met Typeset family today, I should've tried this but I'm always overwhelmed by what is in those undocumented boxes :) p.s. built-in reset button resets to the very initial values, even if you save cdf and new values are preserved, it always goes back to the beginning. Do you find this intuitive?, one could argue, I was surprised for example $\endgroup$ – Kuba Jan 29 '15 at 22:21
  • 1
    $\begingroup$ @Kuba Thanks. From the programming point of view, they are the initial values being reset. It's less clear perhaps for the user in some applications. But Manipulate is not exactly a robust user-application environment. It really seems meant for small, single-purpose demo apps. $\endgroup$ – Michael E2 Jan 29 '15 at 22:32
6
$\begingroup$

Less ambitious:

addbutton[manipulate_] := 
 With[{box = ToBoxes@manipulate}, 
  With[{proc = Cases[box, HoldPattern["Variables" :> _], ∞]}, 
   With[{button = Button["Reset", CompoundExpression["Variables"] /. proc]}, 

    Composition[ToExpression, BoxData][
     box /. {("Body" :> x_) :> "Body" :> Column[{button, x}]}]]]]

addbutton@Manipulate[Dynamic[x], {x, 0, 1}]

CDFDeploy[ToFileName[{$TemporaryDirectory}, "Manipulate.cdf"], %]

More ambitious but:

As Jinxed has noticed, it fails when deployed to CDF with Export or CDFDeploy. However it works by File \ CDF export menu.

The reason is that it produces structure which is not quite correct, enough to work instantly in FE though.

addbutton[manipulate_] := With[{box = ToBoxes @ manipulate},
  With[{
    proc = Cases[box, HoldPattern["Variables" :> _], ∞]        
    },       
   With[{
     button = Button["Reset", CompoundExpression["Variables"] /. proc]
     },        
    Composition[ CellPrint, Cell[#, "Output"] &, BoxData
      ][
     box /. {
      ("Specifications" :> {x__}) :> "Specifications" :> {x, button},
      HoldPattern[Typeset`specs$$ = {y__}] :> (Typeset`specs$$ = {y, {Hold[button], 
        Manipulate`Dump`ThisIsNotAControl}})
      }
     ]
    ]
   ]]

addbutton[
 Manipulate[ Plot[Sin[a x + b], {x, 0, 6}], 
             {{a, 2, "Multiplier"}, 1, 4}, 
             {{b, 0, "Phase Parameter"}, 0, 10}]]

enter image description here

$\endgroup$
  • $\begingroup$ Works really well, except in CDFs. Maybe when wrapped in a DynamicModule's Initialization. $\endgroup$ – Jinxed Jan 29 '15 at 18:19
  • $\begingroup$ I managed to wrap it, but then the CDF shows three copies of the Manipulate. $\endgroup$ – Jinxed Jan 29 '15 at 18:23
  • $\begingroup$ DynamicModule[{}, Null;, SaveDefinitions -> True, Initialization :> addbutton[ Manipulate[ Plot[Sin[a x + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4}, {{b, 0, "Phase Parameter"}, 0, 10}]]] $\endgroup$ – Jinxed Jan 29 '15 at 18:25
  • 1
    $\begingroup$ Otherwise (with your approach applied "pure" and exported) I get nothing more than Hold[Null] {} {} (on three lines). I did the export to CDF using Export["<filename>", %<Out>]. $\endgroup$ – Jinxed Jan 29 '15 at 18:27
  • $\begingroup$ @Jinxed Confirmend, But when you use File\\Export as cdf it works $\endgroup$ – Kuba Jan 29 '15 at 18:31
3
$\begingroup$

If you are not insisting on calling the Manipulate-menu's bookmark, the following approach might do the job:

Manipulate[Column[{Plot[TriangleWave[a x], {x, 0, 1}],
 Button["Custom Reset",
        (* first go back within the notebook *)
        SelectionMove[EvaluationNotebook[], Previous, Cell];
        SelectionMove[EvaluationNotebook[], Previous, Cell];
        (* then reevaluate the Manipulate[]-expression *)
        SelectionEvaluate[EvaluationNotebook[]];]
 }],{{a, 2}, 1, 10}]

However: This approach does not work with Computable Documents.

$\endgroup$
  • $\begingroup$ Thanks. Clever. Thanks also for the advice about CD. Unfortunately that's just my case as I need to make a CDF from my notebook when it's finished (to be used by my students). So I'm afraid I must go for the manual settings of all the control values in the custom button... $\endgroup$ – Luca M Jan 29 '15 at 18:05
  • 1
    $\begingroup$ related ;) $\endgroup$ – Kuba Jan 29 '15 at 18:21
  • $\begingroup$ Sorry. I use SelectionMoveapproach for jumping around within presentation notebooks quite a lot. :( I wasn't my intention to "steal" from you. $\endgroup$ – Jinxed Jan 29 '15 at 18:38
  • 1
    $\begingroup$ @Jinxed It wasn't my intention to mak you feel guilty! :) $\endgroup$ – Kuba Jan 29 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.