º4. The cell bracket is highlighted when the Front End is waiting for the Kernel to finish a dynamic update. For instance:
Manipulate[
If[TrueQ[y > 0],
y = Sin[y];
y = 1.];
a,
{a, 0, 1}
]
Because y
is changed at each update, the updates continue indefinitely. It appears in the Front End that Manipulate[]
is doing nothing, since the new value of y
does not affect the output.
How to fix it? That's depends on the code. Adding the option TrackedSymbols :> {a}
will fix this case. So does localizing y
in a Module[]
inside Manipulate[]
. So does removing the If[]
statement. In cases where this actually arises, localizing y
defeats the purpose of remembering the value of y
from update to update (should that be the purpose); and removing the code is usually not possible, either.
A more complicated example:
Updates indefinitely because data
is changed:
Manipulate[
SeedRandom[a];
data = RandomReal[10, 100]; (* Mathematica treats Random*[] specially *)
data = Sort@data^2; (* this causes the repeated updates *)
lm = LinearModelFit[data, x, x];
Show[
ListPlot[data],
Plot[lm[x], {x, 1, 100}],
PlotRange -> {-21, 121}
],
{a, 0, 1000, 1}
]
Minimal fix:
Manipulate[
SeedRandom[a];
data = Sort@RandomReal[10, 100]^2; (* data not trigger new update *)
lm = LinearModelFit[data, x, x]; (* depends on data which does not need an update *)
Show[
ListPlot[data],
Plot[lm[x], {x, 1, 100}],
PlotRange -> {-21, 121}
],
{a, 0, 1000, 1}
]
My better method (localized assuming I don't need to evaluate data
or lm
outside of Manipulate[]
):
Manipulate[
SeedRandom[a];
With[{data = Sort@RandomReal[10, 100]^2},
With[{lm = LinearModelFit[data, {x, x^2}, x]},
Show[
ListPlot[data],
Plot[lm[x], {x, 1, 100}, PlotStyle -> Gray],
PlotRange -> {-5, 105}
]
]],
{a, 0, 1000, 1}
]
My preferred method (the extra dynamic means that data
and lm
won't be recomputed when the system changes $ControlActiveSetting
from True
to False
when I let go of the a
slider; only the plot needs updating when that happens):
Manipulate[
SeedRandom[a];
With[{data = Sort@RandomReal[10, 100]^2},
With[{lm = LinearModelFit[data, {x, x^2}, x]},
Dynamic@
Show[
ListPlot[data],
Plot[lm[x], {x, 1, 100}, PlotStyle -> Gray],
PlotRange -> {-5, 105}
]
]],
{a, 0, 1000, 1}
]
If I need to use Dynamic[]
/Manipulate[]
to set a global variable such as data
or lm
, then I would use TrackedSymbols
to expressly set the dynamic updating dependencies.