I was wondering if it is possible to use a custom Dynamic[var, function]
inside a Manipulate
. The reason for the need is this:
Suppose you have a time consuming computation like
timeConsuming[x_] := (Pause[1]; x)
and you want to use it in a Manipulate
. Writing
DynamicModule[{y},
Manipulate[
y = timeConsuming[x]; {x, y, other},
{x, 0, 1},
{other, 0, 1}]
]
entails the following issue: you have to wait for timeConsuming
to be computed even when you change other
and leave x
unmodified. A (simple) solution is to code the dynamic by hand:
DynamicModule[{x = 0, y = timeConsuming[0], other = 0},
Panel@Column[{
Grid[{
{"x", Slider[Dynamic[x, (x = #; y = timeConsuming[x]) &], {0, 1}]},
{"other", Slider[Dynamic[other], {0, 1}]}
}],
Dynamic@{x, y, other}
}]
]
This is feasible, but as a drawback makes you renounce all other Module
's conveniences.
I tried coding something like
Module[(* result *), {x, 0, 1, some-suitable-function}]
but had no success in the attempt. A (clean) use of Manipulate
would be much appreciated.
Just to summarize, the question is: How can I make some statements be executed only when some specified controls are touched?
Answer
Is this what you're after?
timeConsuming[x_] := (Pause[1]; x);
DynamicModule[{y},
Manipulate[y = timeConsuming[x]; {Dynamic@x, y, Dynamic@other},
{x, 0, 1}, {other, 0, 1},
SynchronousUpdating -> False]
]
SynchronousUpdating -> False
keeps the front end from being blocked while timeConsuming
is computing, and Dynamic
lets different segments be updated. So x
and other
both update when the sliders are moved, and y
is updated one second after x
is.
Comments
Post a Comment