5
$\begingroup$

I have a notebook to build a Manipulate. I perform an evaluation of the notebook and copy & paste the Manipulate into another notebook that I am using to write the report. I do this several times for different datasets.

What I have found is that all the Manipulates switch to the dataset of the last pasted Manipulate. How can I prevent this from occurring. I was under the impression that Manipulate kept itself separate from other manipulates as it is basically DynamicModule underneath.

The Manipulate has a lot of associated code so I cannot paste it all multiple times into the report notebook. A solution that allows only to copy and paste the output cell is desired.

Minimal example:

data = Range[10];
Manipulate[data[[;; n]], {{n, 1}, 1, 10, 1}, SaveDefinitions -> True]
  1. Run the above and copy & paste the output cell into a new notebook
  2. Change data = Range[10]; to data = Range[10,100,10];.
  3. Run and copy & paste the output cell below the first pasted output cell in the new notebook.
  4. Both Manipulates in the new notebook now show Range[10,100,10] instead of only the second pasted one showing this range.
$\endgroup$
13
  • 1
    $\begingroup$ Try data = Range[10]; With[{data = data}, Manipulate[data[[;; n]], {{n, 1}, 1, 10, 1}, SaveDefinitions -> True] ] $\endgroup$
    – N.J.Evans
    Aug 3, 2016 at 13:30
  • 2
    $\begingroup$ Or DynamicModule[{data = data}, Manipulate[data, {{n, 1}, 1, 10, 1}] ] p.s. if you copy to the notebook with unique context you don't have to change anything because the definitions contain a reference to $CellContex`data not a Global` one. $\endgroup$
    – Kuba
    Aug 3, 2016 at 13:35
  • $\begingroup$ @N.J.Evans That works. (+1) Please post an answer. Any idea why Manipulate is not correctly (as I understand) scoping its variables. We should not have to resort to With? $\endgroup$
    – Edmund
    Aug 3, 2016 at 13:39
  • $\begingroup$ @Kuba That works. (+1) Please post an answer. Any idea why Manipulate is not correctly (as I understand) scoping its variables. We should not have to resort to DynamicModule? $\endgroup$
    – Edmund
    Aug 3, 2016 at 13:40
  • 1
    $\begingroup$ @Edmund It scopes only variables declared in var spec (here n). SaveDefinitions only save others, doesn't localize them additionaly. $\endgroup$
    – Kuba
    Aug 3, 2016 at 13:42

1 Answer 1

3
$\begingroup$

As far as I know, Manipulate doesn't apply any scoping of it's own outside of the controls you define, in your instance n. So you just need to scope the variable at the time you instantiate the Manipulate, my goto for this is With, but Kuba's suggestion using DynamicModule might be more suitable*. e.g. Scoping this way you can run the following in a single cell and get two objects that behave as you expect.

data = Range[10]; 
With[{data = data}, Manipulate[data[[;; n]], {{n, 1}, 1, 10, 1}, SaveDefinitions -> True] ]

data = Range[10,100,10]; 
With[{data = data}, Manipulate[data[[;; n]], {{n, 1}, 1, 10, 1}, SaveDefinitions -> True] ]

*I'm interested to hear from anyone who knows these things well.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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