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'm trying to build an ExampleData browser, so that the "collection"/"type" is selectable in a drop down menu, and the "name" is selectable in another drop down menu. What I have so far:

DynamicModule[ {},
 Panel[
  Column[{
    PopupMenu[Dynamic[q], ExampleData[]],
    (PopupMenu[Dynamic[h], Last[##] & /@ ExampleData[#]]) & /@ 
     Dynamic[q],
    ExampleData[Dynamic[q], Dynamic[h]]
    }]
  ]
 ]

...Only produces a nice error message: ExampleData is not evaluating!

Why isn't ExampleData evaluating the dynamic value of q & h? (i.e. ExampleData["TestImage", "Aerial2"])

share|improve this question
1  
Dynamic@ExampleData[{q, h}] (instead of ExampleData[Dynamic[q], Dynamic[h]]) works (but ... slow ) –  kguler Jul 11 at 20:05
    
Suggest as an answer? (with an explanation as to why this fixes the problem?) –  Alexander Riccio Jul 12 at 0:36
1  
suggestion in my comment fixes one of the problems. As to why it does (1) ExampleData[a,b] should be ExampleData[{a, b}], (2) the error message "Dynamic[q] is not a known collection for ExampleData" gives a hint: Dynamic[q] and q are quite different objects. The more important issue is updating h properly when q is changed so that the {q,h} combination is a valid input for ExampleData so that ExampleData is not called with an input like {"AerialImage", "CatFur"}:). Maybe someone else can come up with a complete solution while I explore some approaches. –  kguler Jul 12 at 1:18

1 Answer 1

up vote 2 down vote accepted

Dynamic is sometimes too aggressive for some purposes. You need to make sure Dynamic updates only when you are ready for new results. Besides, to ensure that Dynamic will have enough time to process your request it is sometimes a good idea to use SynchronousUpdating option. In the solotuion below you will see that instead of calculating the menu options inside the Popup Dynamic, these are calculated in the external Dynamic where the SynchronousUpdating option will avoid aborted calculations.

DynamicModule[{readyQ = False, menu2Options = {}, q = "", h = "", 
  optCnt = 0, pSize = {400, 100}},
 Panel@
  Column[{
    PopupMenu[Dynamic[q, 
      (q = #;
        readyQ = False;
        optCnt++;
        ) &]
     , ExampleData[]
     , Style["Select One", Red]
     ]
    ,
    Dynamic[
     Refresh[
      menu2Options = If[q === "", {}, Last[##] & /@ ExampleData[q]];
      Column[{
        If[ListQ@menu2Options,
         PopupMenu[Dynamic[h,
           (h = #;
             readyQ = True;
             optCnt++;
             ) &
           ]
          , menu2Options
          , Style["Select One", Red]
          ]
         ,
         "No options found."
         ]
        ,
        Pane[
         If[readyQ, ExampleData[{q, h}], "Waiting for your selection."]
         , ImageSize -> Dynamic[pSize]
         , Scrollbars -> Automatic
         ]
        }]
      , TrackedSymbols -> {optCnt}]
     , SynchronousUpdating -> Automatic
     ]
    }
   ]
 ]
share|improve this answer
    
Awesome. Not MAXIMALLY elegant, but awesome. –  Alexander Riccio Jul 23 at 2:46
    
To make my solution more elegant I could have used ActionMenu instead of PopupMenu but that would add some more code and the solution would be a little bit less easy to understand. I think that the community may benefit from a discussion about what you think makes my solution less elegant than possible or expected. Is it that I could have created a more elegant solution or that Mathematica is not allowing me to do so? –  Ariel Sepulveda Jul 25 at 17:19

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.