How to | Evaluate Expressions inside Dynamic or Manipulate
Dynamic and Manipulate have holding attributes that are vital in making them work properly. However, those holding attributes can interfere with other structural operations you may want to perform. This "How to" covers some useful approaches for Dynamic, Manipulate, and other held constructs.
You may want to create this list of sliders more easily, like with Table. The HoldFirst attribute of Dynamic can get in the way, preventing the values of the Table variable from filtering through into the expression. So the following naive attempt fails, and we literally see numbers[[i]] instead of numbers[], numbers[], and so on. Notice the syntax coloring of i, warning you of this danger:
To overcome this, the scoping construct With can be used to immediately replace all instances of the local variable i with its corresponding value:
Getting rid of the InputForm wrapper will give us the formatted sliders:
This same approach can be used in Manipulate, when building sets of controls. The following is a Manipulate whose first slider specifies how many other sliders are shown. The other sliders are wrapped in Panel for visual clarity. Note again the use of With in the Table that creates the sliders:
Here is a similar case, where the number of checkboxes associated with the data variable changes based on the values of other Manipulate controls: