Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am looking for practical uses of the function DynamicWrapper . I read documentation and know whatever it says. I would appreciate a clear example of a useful dynamic interface (an app) that would be hard to implement without DynamicWrapper.

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

Suppose we have a Graphics object which depends on some parameters and a controller with which we want to control these parameters. This could be done easily enough using the second argument of Dynamic, for example

gr[pts_, col_, radius_] := Graphics[{col, Disk[#, radius] & /@ pts}, 
   PlotRange -> {{0, 3}, {0, 3}}, ImageSize -> 200];

contrl = {1, radius};
col = Blue; radius = .1;
pts = RandomReal[3, {10, 2}];

Grid[{{Dynamic[Framed[gr[pts, col, radius]]], 
  Slider2D[Dynamic[cntrl, 
   (cntrl = #; col = Blend[{Red, Blue}, cntrl[[1]]]; radius = cntrl[[2]])&]]
}}]

However, suppose that we also want a switch which switches the coupling between the controller and the plot on and off. With DynamicWrapper this can be done by doing something like this

Grid[{{Dynamic[Framed[gr[pts, col, radius]]], 
  Slider2D[Dynamic[cntrl]], 
  Toggler["Off", {DynamicWrapper["On", 
      col = Blend[{Red, Blue}, cntrl[[1]]]; radius = cntrl[[2]]], 
     "Off"}]
 }}]

Mathematica graphics

By clicking on the label of the slider you can then toggle between coupling or no coupling. The same effect can be achieved without using DynamicWrapper for example

DynamicModule[{state = "Off"}, 
  Grid[{{Dynamic[Framed[gr[pts, col, radius]]], 
    Labeled[Slider2D[Dynamic[cntrl,(cntrl = #; 
         If[state === "On", col = Blend[{Red, Blue}, cntrl[[1]]]; 
         radius = cntrl[[2]]]) &]], 
     Toggler[Dynamic[state], {"On", "Off"}], Top]
  }}]
]

but imho the DynamicWrapper solution is more elegant in this case.

share|improve this answer
    
BTW, the only reason I did not vote for this is that I do not have DynamicWrapper to test it with. Looks nice though. –  Mr.Wizard Jan 25 '12 at 14:05
add comment

How about a 'Next page' button that becomes active only if the user has seen or at least scrolled to a particular part of a page, a disclaimer for instance?


EDIT
As requested: this was what I had in mind

texts = ExampleData["Text"];
i = 1;
imax = texts // Length;
bottomSeen = False;
Panel[
 Column[
  {
   Button["Next page", If[i <= imax, i++, i = 1]; bottomSeen = False;,
     Enabled -> Dynamic[bottomSeen]],
   Dynamic[
    Pane[
     Column[
      {
       ExampleData[texts[[i]]],
       ,
       DynamicWrapper["SEEN THIS", bottomSeen = True]
       }],
     ImageSize -> {500, 150}, Scrollbars -> True, 
     ScrollPosition -> {1, 1}
     ]
    ]
   }
  ]
 ]

Mathematica graphics

There's one problem I didn't anticipate: a DynamicWrapper placed in a Pane is activated as soon as the Pane is visible even when the DynamicWrapper content is scrolled outside the visible window of the Pane. I haven't solved that yet.

share|improve this answer
    
Is it possible to sketch up some code example? –  Vitaliy Kaurov Jan 25 '12 at 17:39
    
@vitaly see update. I'm not there yet. –  Sjoerd C. de Vries Jan 25 '12 at 23:18
    
I think the fact that the cell (rather than the containers like Pane or Panel) displaying the DynamicWrapper object is visible is causing the unwanted update. To see this, you modify the first example in the Basic Examples section of the DynamicWrapper page in the doc center as {DynamicWrapper["xxx", b = 2 a + 1], "\n\n\n\n\n\n\n\n\n\n"} and scroll up/down as you play with the Slider in the following input cell. –  kguler Jan 26 '12 at 5:23
add comment

It seems to me that it is not so much that something would be hard to implement without DynamicWrapper but that using that function offers an alternative and perhaps cosmetically/aesthetically better option.

My typical usage would be where I need to have an expression or compound expression dynamically evaluate (things that come to mind are evaluations that determine the list for a popup menu). Dynamic needs to display so you could just stick a spacer at the end of Dynamic:

DynamicModule[{a, b},

 Column[{
   Style["Heading", "Section"],
   Dynamic[b = 2 a + 1; Spacer[0]],
   Slider[Dynamic[a]],
   Dynamic[{a, b}]
   }]
 ]

or do something like this:

DynamicModule[{a, b},

 Column[{
   DynamicWrapper[Style["Heading", "Section"], b = 2 a + 1],
   Slider[Dynamic[a]],
   Dynamic[{a, b}]
   }]
 ]

but as per documentation this is the same as writing

DynamicModule[{a, b},

 Column[{
   Dynamic[b = 2 a + 1; Refresh[Style["Heading", "Section"], None]],
   Slider[Dynamic[a]],
   Dynamic[{a, b}]
   }]
 ]

So using DynamicWrapper is just a personal preference (IMO).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.