5

I cannot operate on the Dataset I am dynamically filtering. Minimal example follows.

DynamicModule[{ds, class, sex, current},
 Column[{
   PopupMenu[Dynamic@class, {"1st", "2nd", "3rd"}],
   PopupMenu[Dynamic@sex, {"male", "female"}],
   Module[{},
    current = 
     Dynamic@ds[Select[#"class" == class ∧ #"sex" == sex &]];
    DynamicSetting@current[GroupBy["age"]]
    ]
   }]
 ,
 Initialization :> {ds = ExampleData[{"Dataset", "Titanic"}];}
 ]

enter image description here

I have tried Setting instead of DynamicSetting. I also tried moving the Dynamic into the Select on both class and sex.

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Edmund
  • 42,267
  • 3
  • 51
  • 143

1 Answers1

3

A lot of trouble can be avoided by not setting current to a Dynamic object, but assigning a value dynamically to it inside Dynamic.

DynamicModule[{ds, class, sex, current},
 Column[{
   PopupMenu[Dynamic@class, {"1st", "2nd", "3rd"}],
   PopupMenu[Dynamic@sex, {"male", "female"}], 
   Dynamic[current = ds[Select[#"class" == class ∧ #"sex" == sex &]];
    current[GroupBy["age"]]]}], 
 Initialization :> {ds = ExampleData[{"Dataset", "Titanic"}];}]

dds

By setting current = Dynamic@ds[Select[#"class" == class ∧ #"sex" == sex &]] the Head of current becomes Dynamic instead of Dataset. If one really wants to do that, one has to use Setting to get its current value (the Dataset [GroupBy["age"]] should operate on).

DynamicModule[{ds, class, sex, current}, 
 Column[{PopupMenu[Dynamic@class, {"1st", "2nd", "3rd"}], 
   PopupMenu[Dynamic@sex, {"male", "female"}], 
   current = Dynamic@ds[Select[#"class" == class \[And] #"sex" == sex &]];
   Dynamic@Setting@current[GroupBy["age"]]}], 
 Initialization :> {ds = ExampleData[{"Dataset", "Titanic"}];}]

DynamicSetting is not the same as Dynamic@Setting. I consider DynamicSetting to be useful only in conjunction with Evaluate in Place.

Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • May you provide a brief description on why this works? – Edmund May 26 '16 at 01:07
  • It is unclear to me why Dynamic[Setting[_]] works but DynamicSetting[_] doesn't. I thought that is what DynamicSetting was used for. – Edmund May 26 '16 at 01:41
  • I use Module as there are several lines of code there and I prefer to contain them in module inside of Column as a style point. Feels cleaner to me. – Edmund May 26 '16 at 01:48
  • @Edmund This is a simpler version of a similar problem. – Karsten7 May 26 '16 at 08:44
  • Humm. I may have over-simplified my example. Should I edit existing or ask a new question. Your answer does not work for:

    DynamicModule[{ds, class, sex, current}, Column[{ PopupMenu[Dynamic@class, {"1st", "2nd", "3rd"}], PopupMenu[Dynamic@sex, {"male", "female"}], Dynamic[current = filter[class, sex]]; current[GroupBy["age"]] }], Initialization :> {ds = ExampleData[{"Dataset", "Titanic"}]; filter[c_, s_] := ds[Select[#"class" == c \[And] #"sex" == s &]]}]

    – Edmund May 26 '16 at 12:57
  • @Edmund My answer does work. You just misplaced one ]: DynamicModule[{ds, class, sex, current}, Column[{PopupMenu[Dynamic@class, {"1st", "2nd", "3rd"}], PopupMenu[Dynamic@sex, {"male", "female"}], Dynamic[current = filter[class, sex]; current[GroupBy["age"]]]}], Initialization :> {ds = ExampleData[{"Dataset", "Titanic"}]; filter[c_, s_] := ds[Select[#"class" == c \[And] #"sex" == s &]]}]. Invisible Dynamics don't do anything. – Karsten7 May 26 '16 at 14:33
  • Ah, I have to write everything that uses the dynamic symbol in one great big Dynamic. That is going to look a bit of a mess. However, if that is the way it is done. – Edmund May 26 '16 at 14:39
  • Yes!! This works in DynamicModule I am developing. Many thanks. (+1) Will let it sit for a bit: just in case. :-) – Edmund May 26 '16 at 14:41
  • @Edmund A simple and elegant way to separate the big CompoundExpression inside Dynamic is to use a DynamicWrapper: DynamicModule[{ds, class, sex, current}, Column[{PopupMenu[Dynamic@class, {"1st", "2nd", "3rd"}], PopupMenu[Dynamic@sex, {"male", "female"}], DynamicWrapper[ Dynamic[current[GroupBy["age"]]], current = filter[class, sex];]}], Initialization :> {ds = ExampleData[{"Dataset", "Titanic"}]; filter[c_, s_] := ds[Select[#"class" == c \[And] #"sex" == s &]]}] – Karsten7 May 26 '16 at 14:45
  • Very nice addition! – Edmund May 26 '16 at 15:16