5
$\begingroup$

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

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.

$\endgroup$
10
  • $\begingroup$ May you provide a brief description on why this works? $\endgroup$
    – Edmund
    May 26, 2016 at 1:07
  • $\begingroup$ It is unclear to me why Dynamic[Setting[_]] works but DynamicSetting[_] doesn't. I thought that is what DynamicSetting was used for. $\endgroup$
    – Edmund
    May 26, 2016 at 1:41
  • $\begingroup$ 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. $\endgroup$
    – Edmund
    May 26, 2016 at 1:48
  • $\begingroup$ @Edmund This is a simpler version of a similar problem. $\endgroup$
    – Karsten7
    May 26, 2016 at 8:44
  • $\begingroup$ 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 &]]}] $\endgroup$
    – Edmund
    May 26, 2016 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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