3
$\begingroup$

I want to use Select for a more complex dataset query. Part of the query shall be dynamic, depending on the user input. The Select part looks something like

Select[
 #Group == cGroup
   && #"Component Type" == component2
   && NumberQ@#[xaxisElement]
   && NumberQ@#[yaxisElement]
   && (#Reference == "ref1" || #Reference == "ref2" || #Reference == "ref3")
  &]

The last line with the references shall be dynamically produced, depending which references the user wants to be included. I found that I can produce such a list with

Apply[Or, Equal[Slot["Reference"], #] & /@ a]

where a represents the list of references that the user chose, e.g.

a={"refer 1","refer 2","refer 3","refer 4"}

However, when I include the above code in Select, it does not work; i.e.

Select[
 #Group == cGroup
   && #"Component Type" == component2
   && NumberQ@#[xaxisElement]
   && NumberQ@#[yaxisElement]
   && (Apply[Or, Equal[Slot["Reference"], #] & /@ a])
  &]

does not work. When I copy & past the output from the Apply[...] command, it works. I have no clue, why and how I can get it to work so the user generate list is part of Select.

Addition: Here is a simplified example how the program looks like

data = {{"Group", "Component Type", "Reference"}, {"g1", "a", 
    "ref1"}, {"g1", "a", "ref2"}, {"g1", "b", "ref1"}, {"g1", "b", 
    "ref1"}, {"g1", "b", "ref2"}, {"g1", "a", "ref3"}, {"g2", "b", 
    "ref1"}, {"g2", "b", "ref1"}, {"g2", "a", "ref3"}, {"g3", "a", 
    "ref1"}, {"g3", "b", "ref3"}, {"g3", "d", "ref2"}};
dataset = Dataset[AssociationThread[First@data, #] & /@ Rest@data];

Query[Select[#Group == "g1" && #"Component Type" == 
      "a" && (#Reference == "ref1" || #Reference == 
        "ref2") &]]@dataset

Now what I would like to have is that the Reference part in the parentheses are generated depending on user input. As it is, datasets with ref1 and ref2 are displayed. But when a user choses only ref1, datasets with only ref1 should be displayed. Or, if the user chooses ref1, ref2 and ref3, all datasets with these references are displayed (plotted, to be precise).

$\endgroup$
6
  • $\begingroup$ Reference is a named slot of what Association? $\endgroup$ Sep 15, 2014 at 17:20
  • $\begingroup$ Yes, it's a named slot of Association. Basically, datasets from certain references shall be selectable. $\endgroup$ Sep 15, 2014 at 18:01
  • $\begingroup$ What I mean is, Slot : Association :: "Reference" : ? -- in your application. $\endgroup$ Sep 15, 2014 at 18:03
  • $\begingroup$ I have a Dataset and one Column is labelled Reference (others e.g. Group, Component Type, etc.). Certain datasets from Group, Component Type, ... and Reference are chosen. References are displayed with checkboxes. The user ticks the checkboxes and only those References will be displayed in a plot. Hence, the list for the References need to be build each time. -- If this is still confusing, I could include a simplified dataset. $\endgroup$ Sep 15, 2014 at 18:13
  • $\begingroup$ That'd help b/c as-is, Apply[...] doesn't work as Slot expects an Association, which a is not. $\endgroup$ Sep 15, 2014 at 19:07

1 Answer 1

3
$\begingroup$

How's this:

refQuery[references_List] := 
 With[{refs = 
    references // Map[Equal[Slot["Reference"], #] &] // Apply[Or]},
  Off[Function::slot1];
  Query[Select[#Group == "g1" && #"Component Type" == "a" && refs &]]
  ]

refQuery[{"ref1", "ref3"}]@dataset

(* {<|"Group" -> "g1", "Component Type" -> "a", 
  "Reference" -> "ref1"|>, <|"Group" -> "g1", "Component Type" -> "a",
   "Reference" -> "ref3"|>} *) 
$\endgroup$
1
  • $\begingroup$ This looks great! Thanks a lot. I don't yet fully understand your code, but that's probably the reason I couldn't find the solution myself. $\endgroup$ Sep 16, 2014 at 8:53

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.