I need to have a few Manipulates in different cells synchronize on a few variables.
I have a report with several Manipulates in different sections showing different views/summaries of the data. Some of the selection variables are common to all Manipulates. When the reader updates one of these common in any of the Manipulates I would like all Manipulates to update the value of that variable and execute (or maybe execute on "MouseMoved" so the CDF doesn't crawl).
Using a Notebook level variable (I have CellContext set to Notebook in Global Preferences) I can see that the sync variable is syncing but I can't seem to get the Manipulates to use it.
sync = 1;
DynamicModule[{d = Range[10]},
Manipulate[
Column[{d[[;; j]], sync}],
{{j, 1}, 1, 10, 1,
TrackingFunction -> {(j = sync) &, (j = #) &, (j = #; sync = #) &}}
],
SaveDefinitions -> True
]
DynamicModule[{d = Range[10, 100, 10]},
Manipulate[
Column[{d[[;; i]], sync}],
{{i, 1}, 1, 10, 1,
TrackingFunction -> {(i = sync) &, (i = #) &, (i = #; sync = #) &}}
],
SaveDefinitions -> True
]
There is the new Channel-Based Communication in version 11 that might help but I don't have that installed yet (and it is experimental). I'll take an 10.4.1 or an 11 solution but I need it to work in CDF. Not certain there is Player Pro for version 11 yet.
Answer
Another approach is to localize the context of the CDF and use LocalizeVariables -> False in Manipulate. You have to remember to manually localize controls in the Manipulate that you don't want to share.
SetOptions[EvaluationNotebook[], CellContext -> Notebook]
DynamicModule[{d = Range[10], offset},
Manipulate[d[[;; j]] + offset,
{{j, 1}, 1, 10, 1}, {offset, 0, 1},
LocalizeVariables -> False],
SaveDefinitions -> True]
DynamicModule[{d = Range[10, 100, 10], offset},
Manipulate[d[[;; j]] + offset,
{{j, 1}, 1, 10, 1}, {offset, 0, 1},
LocalizeVariables -> False],
SaveDefinitions -> True]

I'm not sure which way will be too much trouble, maintaining the TrackingFunctions (e.g. Kuba's answers) or the scope of variables. Someone on the site once said Manipulate is a beast (in part because it automatically rewrites your code, which is probably why the OP had trouble). The typical advice here when things get complicated is to abandon Manipulate and do it all yourself with DynamicModule[].
Comments
Post a Comment