Chapter 15. Interactivity

I’m alive Oh oh, so alive I’m alive Oh oh, so alive ... My head is full of magic, baby And I can share this with you The feel I’m on top again, baby That’s got everything to do with you

The pièce de résistance of Mathematica 6 is its dynamic interactivity features. These features forced Wolfram to completely rethink and redesign its frontend. This had the unfortunate consequence of breaking many notebooks from version 5 and earlier, especially those that used graphics. However, it is my opinion that the gain was well worth the pain!

The interactive features of Mathematica 6 are even more impressive when one considers that they sit on relatively few new functions. The centerpiece of interactivity is the function Manipulate. Think of Manipulate as a very intelligent user-interface generator. Manipulate’s power comes by virtue of its ability to take any Mathematica expression plus a declarative description of the expression’s variables and generate a mini embedded GUI within the notebook for interacting with that expression. Of course, there are always caveats, and an important feature of this chapter is to help you get the best possible results with nontrivial Manipulate use cases.

The first five recipes of this chapter are intended to gradually introduce the reader to Manipulate by demonstrating increasingly sophisticated examples. These recipes are not necessarily intended for direct use but rather to illustrate the basic features and generality of Manipulate. Each recipe highlights a feature of Manipulate or a subtlety of its use in a particular context. Animate is a relative of Manipulate that puts its interactive features in autonomous mode. 15.15 Animating an Expression focuses on Animate and shows how animations can be exported to Flash and other Web-friendly formats.

Many users will never need anything beyond Manipulate, but more advanced applications require you to dig deeper and understand lower-level dynamic primitive functions called Dynamic, DynamicModule, and DynamicWrapper. 15.4 Creating Expressions for Which Value Dynamically Updates shows how Dynamic is used in conjunction with Manipulate to achieve better performance or smoother operation. DynamicModule is a preferred alternative to Module when working with dynamic content, and I use it liberally before introducing it formally. The initial usage does not require you to know more than its function as a scoping construct. 15.11 Improving Performance of Manipulate by Segregating Fast and Slow Operations illustrates the intimate relationship between Manipulate and DynamicModule and shows why you often want to use DynamicModule directly. Many useful dynamic techniques require the use of DynamicWrapper but, unfortunately (as of version 7), this important function is undocumented in the help system. 15.8 Using Scratch Variables with DynamicModule to Balance Speed Versus Space, 15.11 Improving Performance of Manipulate by Segregating Fast and Slow Operations, and 15.16 Creating Custom Interfaces show some interesting use cases for this hidden gem.

This solution is strictly intended as a simple introduction to Manipulate. As it stands, it is not very practical because the variables are displayed rather than used to compute. Still, there are some important concepts.

The first concept is that Manipulate will automatically choose a control type based on the structure of the constraints you place on a variable’s value. The most common control is a slider. It is chosen when a variable is specified with a minimum and maximum value. Out[3] below shows three variations of this idea. The second example uses a specified increment, and the third adds an initial value.

Discussion

When a multiple-choice list is specified, you will get either a series of buttons or a drop-down list, depending on the number of choices.

Discussion

When a variable is unconstrained or just specified with an initial value, Manipulate infers an edit control. In the first case, the variable begins with a null value, so it is probably a good idea to provide an initial value.

Discussion

A second concept, illustrated in Out[6], is that a single variable can be bound to multiple controls. This has the effect of tying the controls together so a change in one control changes the variable and is automatically reflected in the other controls bound to that variable. It’s possible in this circumstance to violate the constraints of one of the controls. In this case, Manipulate will display a red area in the control that has the violated constraint.

Discussion

A third concept is the ability to provide an arbitrary label by specifying the label after the initial value. The label can be any Mathematica expression.

Discussion

Here are a few examples to reinforce the idea that any aspect of an expression can be manipulated. In Out[9] on Discussion, both of the function’s integration limits are variable. In Out[10] on Discussion, every aspect of the expression, including its display form, is subject to user manipulation. Finally, in Out[11] on 15.3 Manipulating a Plot, you see that tables of values can be dynamically generated and that Manipulate will adjust the display area to accommodate the additional rows. The ability of Manipulate to mostly do the right thing is immensely liberating: it allows you to focus on the concept you are illustrating rather than the GUI programming.

Discussion
Discussion

See 15.7 Using DynamicModule As a Scoping Construct in Interactive Notebooks for an explanation of why DynamicModule is used in the Ohm’s law example.

DynamicModule is similar to Module in that it restricts the scope of variables, but DynamicModule has the additional behavior of preserving the values of the local variables in the output so that they are retained between Mathematica sessions. Further, if you copy and paste the output of a DynamicModule, the values of the pasted copy are also localized in the copy, leaving the original unchanged as the copy varies.

Solution

The dynamic plot on Discussion was copied from Out[35] above, pasted here, and then the locators manipulated. Each variable has its own independent state that will be retained after Mathematica is shut down and restarted with this notebook. This works because the values are bundled with the expression that underlies the output cells of a dynamic module.

Solution

DynamicWrapper is further discussed in the DynamicWrapper: A Useful Undocumented Function sidebar on DynamicWrapper: A Useful Undocumented Function and the Discussion section of 15.11 Improving Performance of Manipulate by Segregating Fast and Slow Operations.

Variables defined in the first argument of a DynamicModule or as control variables in a Manipulate have their scope restricted to the resulting output cell. This concept is explained in 15.7 Using DynamicModule As a Scoping Construct in Interactive Notebooks. Generally, this is exactly the behavior you want when using Manipulate. An obvious exception is when you want to create a more complex application composed of multiple notebooks (a palette is implemented as a notebook). The whimsical term wormhole is used to suggest the sharing of scope between different physical locations (e.g., output cells).

Here is an example that uses the same technique as the solution but with DynamicModule instead of Manipulate and multiple output cells instead of a palette. Each time the button is pressed, a new cell is printed that inherits the scope from the original DynamicModule.

Discussion

The "Advanced Dynamic Functionality" tutorial ( http://bit.ly/3u8fXo ) explains some of the technical details underlying DynamicModule wormholes. It hints at the ability to link up arbitrary existing DynamicModules but, unfortunately, provides no additional information.

In addition to OpenerView and PaneSelector, there is a whole family of controls for managing limited screen real estate. These include FlipView, MenuView, SlideView, and TabView. I provide a sample of each without going into much detail because they are fairly self-explanatory and follow the same basic syntax.

A FlipView cycles through a list of expressions as you click on the output. Here I use FlipView over a list of graphics. Click on the graphic to see the next in the series.

Discussion

SlideView is similar to FlipView but uses VCR-style controls to give more control of the progression.

Discussion

A MenuView allows you random access to the items via a menu that you specify as a list of rules: MenuView[{lbl1→expr1, label2→expr2, ...}]. This is similar syntax to that used by PaneSelector in the solution. Don’t be afraid to build up these expressions using a bit of functional programming as I do here, especially if it cuts down on repetition. In Out[78] below, I use the Head of each graphic primitive as the label for convenience, but you can also provide the label explicitly, as in Out[79] on Discussion, which builds the list of rules using Inner.

Discussion
Discussion

TabView is similar to MenuView but uses the familiar tabbed folder theme that has become popular in a variety of modern interfaces, including most web browsers.

Discussion

Clearly these controls can be mixed, combined with Manipulate, or used alone to create an unlimited variety of sophisticated interfaces. For example, here is a tabbed set of Manipulates.

Discussion

Contrast this to a single Manipulate that can switch between a TabView or a MenuView, or even one that lets you switch back and forth. This is actually a useful technique when building an interface for someone’s approval. You can switch among various design choices without touching the code.

Discussion